// // EMRefreshViewController.m // DXStudio // // Created by XieYajie on 12/10/2017. // Copyright © 2017 dxstudio. All rights reserved. // #import "EMRefreshViewController.h" #import "MJRefresh.h" @interface EMRefreshViewController () @end @implementation EMRefreshViewController - (void)viewDidLoad { [super viewDidLoad]; // Uncomment the following line to preserve selection between presentations. if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) { [self setEdgesForExtendedLayout:UIRectEdgeNone]; } _page = 0; _showRefreshHeader = NO; _showRefreshFooter = NO; self.view.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.tableView]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - setter //- (void)setRefreshHeaderColor:(UIColor *)aColor //{ // if (_showRefreshHeader && self.tableView.mj_header) { // [(MJRefreshNormalHeader *)self.tableView.mj_header setRefreshHeaderColor:aColor]; // } //} - (void)setShowRefreshHeader:(BOOL)showRefreshHeader { if (_showRefreshHeader != showRefreshHeader) { _showRefreshHeader = showRefreshHeader; if (_showRefreshHeader) { __weak typeof(self) weakSelf = self; self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ [weakSelf tableViewDidTriggerHeaderRefresh]; }]; self.tableView.mj_header.accessibilityIdentifier = @"refresh_header"; // header.updatedTimeHidden = YES; } else{ [self.tableView setMj_header:nil]; } } } - (void)setShowRefreshFooter:(BOOL)showRefreshFooter { if (_showRefreshFooter != showRefreshFooter) { _showRefreshFooter = showRefreshFooter; if (_showRefreshFooter) { __weak typeof(self) weakSelf = self; self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ [weakSelf tableViewDidTriggerFooterRefresh]; }]; self.tableView.mj_footer.accessibilityIdentifier = @"refresh_footer"; } else{ [self.tableView setMj_footer:nil]; } } } #pragma mark - getter - (NSMutableArray *)dataArray { if (_dataArray == nil) { _dataArray = [NSMutableArray array]; } return _dataArray; } - (UIView *)defaultFooterView { if (_defaultFooterView == nil) { _defaultFooterView = [[UIView alloc] init]; } return _defaultFooterView; } - (UITableView *)tableView { if (_tableView == nil) { _tableView = [[UITableView alloc] init]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.tableFooterView = self.defaultFooterView; } return _tableView; } - (BOOL)isHeaderRefreshing { BOOL isRefreshing = NO; if (self.showRefreshHeader) { isRefreshing = self.tableView.mj_header.isRefreshing; } return isRefreshing; } - (BOOL)isFooterRefreshing { BOOL isRefreshing = NO; if (self.showRefreshFooter) { isRefreshing = self.tableView.mj_footer.isRefreshing; } return isRefreshing; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return 0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"UITableViewCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // Configure the cell... return cell; } #pragma mark - public refresh - (void)autoTriggerHeaderRefresh { if (self.showRefreshHeader) { [self tableViewDidTriggerHeaderRefresh]; } } - (void)tableViewDidTriggerHeaderRefresh { } - (void)tableViewDidTriggerFooterRefresh { } - (void)tableViewDidFinishTriggerHeader:(BOOL)isHeader reload:(BOOL)reload { __weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ if (reload) { [weakSelf.tableView reloadData]; } if (isHeader) { [weakSelf.tableView.mj_header endRefreshing]; } else{ [weakSelf.tableView.mj_footer endRefreshing]; } }); } @end