IVTMyBlackListViewController.m 4.81 KB
//
//  IVTMyBlackListViewController.m
//  IVTMeLiveTwo
//
//  Created by XRG on 16/8/23.
//  Copyright © 2016年 QLQ. All rights reserved.
//

#import "IVTMyBlackListViewController.h"
#import "IVTMyBlackListTableViewCell.h"

@interface IVTMyBlackListViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *requestDataArray;
    UITableView *myTableView;
}
@end

@implementation IVTMyBlackListViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setNavigationBar];
    
    requestDataArray = [[NSMutableArray alloc]initWithCapacity:10];

    __weak __typeof(self)weakSelf = self;
    myTableView = [[UITableView alloc] init];
    myTableView.backgroundColor = [UIColor IVTBackgroundColor];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    myTableView.showsHorizontalScrollIndicator = NO;
    myTableView.showsVerticalScrollIndicator = NO;
    myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    myTableView.tableFooterView = [UIView new];
    myTableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    [self.view addSubview:myTableView];
    [myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view.mas_left);
        make.top.equalTo(weakSelf.view.mas_top);
        make.right.equalTo(weakSelf.view.mas_right);
        make.bottom.equalTo(weakSelf.view.mas_bottom);
    }];

    [myTableView.mj_header beginRefreshing];
}

- (void)setNavigationBar{
    self.navigationItem.title = @"黑名单";
}

- (void)doRequest{
    [[IVTNetworkAPIClient sharedClient] getBlackListSuccess:^(NSDictionary *successData) {
        [myTableView.mj_header endRefreshing];
        [requestDataArray addObjectsFromArray:[successData objectForKey:@"list"]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.66 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [myTableView reloadData];
        });
    } error:^(NSDictionary *successData) {
        [myTableView.mj_header endRefreshing];
        NSString *msg = successData[@"description"];
        [HUDUtil showError:msg];
    } failure:^(NSError *failureError) {
        [myTableView.mj_header endRefreshing];
        [HUDUtil showError:@"网络异常"];
    }];
}

- (void)refreshData{
    [requestDataArray removeAllObjects];
    [self doRequest];
}
#pragma mark - table datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 59;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return requestDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    IVTMyBlackListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"blackListCell"];
    if (!cell) {
        cell = [[IVTMyBlackListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"blackListCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.entity = [requestDataArray objectAtIndex:indexPath.row];
    
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self deleteRow:indexPath];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"移除";
}

- (void)deleteRow:(NSIndexPath *)indexPath{
    [[IVTNetworkAPIClient sharedClient] cancelLaHeiForUserId:[[requestDataArray objectAtIndex:indexPath.row] valueForKey:@"id"] success:^(NSDictionary *successData) {
        [SVProgressHUD setMinimumDismissTimeInterval:2];
        [SVProgressHUD showSuccessWithStatus:@"移除成功"];
        [requestDataArray removeObjectAtIndex:indexPath.row];
        [myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
    } error:^(NSDictionary *successData) {
        
    } failure:^(NSError *failureError) {
        
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end