gliubc
12/16/2018 - 9:22 AM

LNCustomTableViewController

//
//  LNCustomTableViewController.h
//  LNMobileProject
//
//  Created by Liu Cheng on 2018/5/25.
//

#import <UIKit/UIKit.h>

@interface LNCustomTableViewController : LNBaseVC

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *dataArray;
@property (assign, nonatomic) NSInteger page;

- (void)loadData;
- (void)loadDataWithUrl:(NSString *)url params:(NSDictionary *)params success:(void(^)(id json))success;
- (void)reloadData;

@end
//
//  LNCustomTableViewController.m
//  LNMobileProject
//
//  Created by Liu Cheng on 2018/5/25.
//

#import "LNCustomTableViewController.h"

@interface LNCustomTableViewController () <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) EmptyDataSource *emptyDataSource;
@property (assign, nonatomic) BOOL isLoading;

@end

@implementation LNCustomTableViewController

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray new];
    }
    return _dataArray;
}

- (NSInteger)page {
    if (!_page) {
        _page = 1;
    }
    return _page;
}

- (EmptyDataSource *)emptyDataSource{
    if (!_emptyDataSource) {
        _emptyDataSource = [[EmptyDataSource alloc] init];
    }
    return _emptyDataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    if (!self.tableView) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
        tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        self.view = self.tableView = tableView;
    }

    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.emptyDataSetSource = self.emptyDataSource;
    self.tableView.emptyDataType = EmptyDataTypeList;
    self.tableView.tableFooterView = [UIView new];
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self reloadData];
    }];
    header.lastUpdatedTimeLabel.hidden = YES;
    self.tableView.mj_header = header;
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (self.dataArray.count) {
            self.page++;
        } else {
            self.page = 1;
        }
        [self loadData];
    }];
}

- (void)reloadData {
    self.page = 1;
    [self loadData];
}

- (void)loadData {
    [self.dataArray addObject:[NSDate date].description];
    [self.tableView reloadData];
    [self endRefresh];
}

- (void)endRefresh {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

- (void)loadDataWithUrl:(NSString *)url params:(NSDictionary *)params success:(void(^)(id json))success {
    if (self.isLoading) {
        return;
    }
    self.isLoading = YES;

    if (![self.tableView.mj_header isRefreshing] && ![self.tableView.mj_footer isRefreshing]) {
        [SVProgressHUD show];
    }
    [[[LNNetWorkAPI alloc] initWithUrl:url parameters:params] startWithBlockSuccess:^(__kindof LCBaseRequest *request) {
        self.isLoading = NO;
        [SVProgressHUD dismiss];
        [self endRefresh];
        
        if (self.page == 1) {
            [self.dataArray removeAllObjects];
        }

        id json = request.responseJSONObject;
        if ([json[@"status"] integerValue] != 1) {
            [SVProgressHUD showInfoWithStatus:json[@"msg"]];
            return;
        }
        if (self.page > 1 || ([json[@"data"] respondsToSelector:@selector(count)] && [json[@"data"] count])) {
            [self.tableView removeEmptyDataSet];
        } else {
            [self.tableView reloadEmptyDataSet];
        }
        if (success) {
            success(json);
        }
    } failure:^(__kindof LCBaseRequest *request, NSError *error) {
        self.isLoading = NO;
        [SVProgressHUD dismiss];
        [self endRefresh];
        [SVProgressHUD showInfoWithStatus:error.domain];
    }];
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    
    // Configure the cell...
    cell.textLabel.text = self.dataArray[indexPath.row];

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Remove separator inset
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }

    // Explicitly set your cell's layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

@end