// // GHRefreshTableView.m // GameHelper // // Created by 青秀斌 on 16/12/29. // Copyright © 2016年 kylincc. All rights reserved. // #import "GHRefreshTableView.h" #import "GHRefreshHeader.h" #import "GHRefreshFooter.h" #import "UIView+GHBlank.h" #import "AFNetworkReachabilityManager.h" #import "UITableView+HeadSet.h" #import #define NET_CONNECT_ERROR @"当前网络不给力,请稍后重试" @interface GHRefreshTableView () @property (strong, nonatomic) NSMutableArray *dataArray; @property (assign, nonatomic) NSUInteger pageIndex; @property (assign, nonatomic) NSUInteger offset; @end @implementation GHRefreshTableView - (instancetype)init { if (self = [super init]) { [self awakeFromNib]; } return self; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self awakeFromNib]; } return self; } - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style { if (self = [super initWithFrame:frame style:style]) { [self awakeFromNib]; } return self; } - (void)awakeFromNib { [super awakeFromNib]; self.pageIndex = 1; self.offset = 0; self.pageSize = 20; self.allowShowMore = YES; self.allowShowBlank = YES; self.allowShowNoNetworkBlank = YES; self.allowExternalControlShowBlank = NO; self.allowShowBlankWithHeaderTableView = NO; self.blankImage = nil; self.blankTitle = nil; self.blankMessage = nil; self.offsetY = 0; self.carRefreshBackgroundType = kCarRefreshBackgroundTypeDoubleCircle;//下拉加载动画 } - (void)setRefreshDelegate:(id)refreshDelegate { _refreshDelegate = refreshDelegate; if ([self.refreshDelegate respondsToSelector:@selector(tableView:pageIndex:offset:pageSize:success:failure:)]) { [self addRefreshHeader]; } } - (void)setAllowShowMore:(BOOL)allowShowMore { _allowShowMore = allowShowMore; if (self.mj_footer) { [self.mj_footer removeFromSuperview]; self.mj_footer = nil; } } -(void)removeHeaderView{ if (self.mj_header) { [self.mj_header removeFromSuperview]; self.mj_header = nil; } if (self.refreshHeaderView) { [self.refreshHeaderView removeFromSuperview]; [self setRefreshHeaderView:nil]; } } /**********************************************************************/ #pragma mark - Private /**********************************************************************/ - (void)addRefreshHeader { if (self.mj_header) { return; } GHRefreshHeader *header = [GHRefreshHeader headerWithRefreshingBlock:^{ NSUInteger pageIndex = 1; NSUInteger offset = 0; WS(weakSelf); [self.refreshDelegate tableView:self pageIndex:pageIndex offset:offset pageSize:self->_pageSize success:^(NSArray *list, BOOL hasNext) { [weakSelf.mj_header endRefreshing]; //添加RefreshFooter [weakSelf addRefreshFooter]; if (hasNext) { [weakSelf.mj_footer endRefreshing]; } else { [weakSelf.mj_footer endRefreshingWithNoMoreData]; } //加载数据 weakSelf.pageIndex = pageIndex; weakSelf.offset = offset; if (weakSelf.reloadView) { weakSelf.reloadView(list); } else { weakSelf.dataArray = [list mutableCopy]; [weakSelf reloadData]; } //显示占位图 if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) { weakSelf.mj_footer.hidden = YES; if(self.allowShowBlankWithHeaderTableView){ [weakSelf addHeaderView:weakSelf.blankImage?:@"no_data" title:weakSelf.blankTitle?:@"暂无数据"]; }else{ [weakSelf showBlankWithImage:weakSelf.blankImage?:@"no_data" title:weakSelf.blankTitle?:@"暂无数据" message:weakSelf.blankMessage action:nil offsetY:weakSelf.offsetY]; } } else { [weakSelf dismissBlank]; if(!weakSelf.allowExternalControlShowBlank){ weakSelf.mj_footer.hidden = NO; [weakSelf setHeadIsNil]; }else{ weakSelf.mj_footer.hidden = YES; } } } failure:^(NSError *error) { [weakSelf.mj_header endRefreshing]; //显示占位图 NSString *blankTitle = error.localizedDescription?:weakSelf.blankTitle; if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) { weakSelf.mj_footer.hidden = YES; BOOL isHaveNet = NO; if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown || AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) { isHaveNet = YES; } if (weakSelf.allowShowNoNetworkBlank && isHaveNet) { if(self.allowShowBlankWithHeaderTableView){ [weakSelf addHeaderView:@"no_network" title:NET_CONNECT_ERROR]; }else{ [weakSelf showBlankWithImage:@"no_network" title:NET_CONNECT_ERROR message:nil action:nil offsetY:weakSelf.offsetY]; } } else { if(self.allowShowBlankWithHeaderTableView){ [weakSelf addHeaderView:weakSelf.blankImage?:@"failed_to_load" title:blankTitle?:@"加载失败"]; }else{ [weakSelf showBlankWithImage:weakSelf.blankImage?:@"failed_to_load" title:blankTitle?:@"加载失败" message:weakSelf.blankMessage action:nil offsetY:weakSelf.offsetY]; } } } else { [weakSelf dismissBlank]; if(!weakSelf.allowExternalControlShowBlank){ weakSelf.mj_footer.hidden = NO; [weakSelf setHeadIsNil]; }else{ weakSelf.mj_footer.hidden = YES; } // [ShowtipTool showErrorWithStatus:blankTitle]; } }]; }carRefreshBackgroundType:self.carRefreshBackgroundType]; self.refreshHeaderView = header; self.mj_header = header; } - (void)addRefreshFooter { if (!self.allowShowMore) { return; } if (self.mj_footer) { return; } GHRefreshFooter *footer = [GHRefreshFooter footerWithRefreshingBlock:^{ NSUInteger pageIndex = self.pageIndex + 1; NSInteger offset = self.dataArray.count + 1; WS(weakSelf); [self.refreshDelegate tableView:self pageIndex:pageIndex offset:offset pageSize:_pageSize success:^(NSArray * _Nullable list, BOOL hasNext) { if (hasNext) { [weakSelf.mj_footer endRefreshing]; } else { [weakSelf.mj_footer endRefreshingWithNoMoreData]; } //加载数据 weakSelf.pageIndex = pageIndex; weakSelf.offset = offset; if (weakSelf.reloadView) { weakSelf.reloadView(list); } else { [weakSelf.dataArray addObjectsFromArray:list]; [weakSelf reloadData]; } //显示占位图 if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) { weakSelf.mj_footer.hidden = YES; if(self.allowShowBlankWithHeaderTableView){ [weakSelf addHeaderView:weakSelf.blankImage?:@"no_data" title:weakSelf.blankTitle?:@"暂无数据"]; }else{ [weakSelf showBlankWithImage:weakSelf.blankImage?:@"no_data" title:weakSelf.blankTitle?:@"暂无数据" message:weakSelf.blankMessage action:nil offsetY:weakSelf.offsetY]; } } else { [weakSelf dismissBlank]; if(!weakSelf.allowExternalControlShowBlank){ weakSelf.mj_footer.hidden = NO; [weakSelf setHeadIsNil]; }else{ weakSelf.mj_footer.hidden = YES; } } } failure:^(NSError *error) { [weakSelf.mj_footer endRefreshing]; //显示占位图 NSString *blankTitle = error.localizedDescription?:weakSelf.blankTitle; if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) { weakSelf.mj_footer.hidden = YES; BOOL isHaveNet = NO; if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown || AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) { isHaveNet = YES; } if (weakSelf.allowShowNoNetworkBlank && isHaveNet) { if(self.allowShowBlankWithHeaderTableView){ [weakSelf addHeaderView:@"no_network" title:NET_CONNECT_ERROR]; }else{ [weakSelf showBlankWithImage:@"no_network" title:NET_CONNECT_ERROR message:nil action:nil offsetY:weakSelf.offsetY]; } } else { if(self.allowShowBlankWithHeaderTableView){ [weakSelf addHeaderView:weakSelf.blankImage?:@"failed_to_load" title:blankTitle?:@"加载失败"]; }else{ [weakSelf showBlankWithImage:weakSelf.blankImage?:@"failed_to_load" title:blankTitle?:@"加载失败" message:weakSelf.blankMessage action:nil offsetY:weakSelf.offsetY]; } } } else { [weakSelf dismissBlank]; if(!weakSelf.allowExternalControlShowBlank){ weakSelf.mj_footer.hidden = NO; [weakSelf setHeadIsNil]; }else{ weakSelf.mj_footer.hidden = YES; } // [ShowtipTool showErrorWithStatus:blankTitle]; } }]; }]; self.mj_footer = footer; } /**********************************************************************/ #pragma mark - Public /**********************************************************************/ - (void)refreshData { [self.mj_header beginRefreshing]; } - (void)refreshDataNoAnimation{ NSUInteger pageIndex = 1; NSInteger offset = 0; WS(weakSelf); [self.refreshDelegate tableView:self pageIndex:pageIndex offset:offset pageSize:_pageSize success:^(NSArray *list, BOOL hasNext) { [weakSelf.mj_header endRefreshing]; //添加RefreshFooter [weakSelf addRefreshFooter]; if (hasNext) { [weakSelf.mj_footer endRefreshing]; } else { [weakSelf.mj_footer endRefreshingWithNoMoreData]; } //加载数据 weakSelf.pageIndex = pageIndex; weakSelf.offset = offset; if (weakSelf.reloadView) { weakSelf.reloadView(list); } else { weakSelf.dataArray = [list mutableCopy]; [weakSelf reloadData]; } //显示占位图 if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) { weakSelf.mj_footer.hidden = YES; if(self.allowShowBlankWithHeaderTableView){ [weakSelf addHeaderView:weakSelf.blankImage?:@"no_data" title:weakSelf.blankTitle?:@"暂无数据"]; }else{ [weakSelf showBlankWithImage:weakSelf.blankImage?:@"no_data" title:weakSelf.blankTitle?:@"暂无数据" message:weakSelf.blankMessage action:nil offsetY:weakSelf.offsetY]; } } else { [weakSelf dismissBlank]; if(!weakSelf.allowExternalControlShowBlank){ weakSelf.mj_footer.hidden = NO; [weakSelf setHeadIsNil]; }else{ weakSelf.mj_footer.hidden = YES; } } } failure:^(NSError *error) { [weakSelf.mj_header endRefreshing]; //显示占位图 NSString *blankTitle = error.localizedDescription?:weakSelf.blankTitle; if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) { weakSelf.mj_footer.hidden = YES; BOOL isHaveNet = NO; if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown || AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) { isHaveNet = YES; } if (weakSelf.allowShowNoNetworkBlank && isHaveNet) { if(self.allowShowBlankWithHeaderTableView){ [weakSelf addHeaderView:@"no_network" title:NET_CONNECT_ERROR]; }else{ [weakSelf showBlankWithImage:@"no_network" title:NET_CONNECT_ERROR message:nil action:nil offsetY:weakSelf.offsetY]; } } else { if(self.allowShowBlankWithHeaderTableView){ [weakSelf addHeaderView:weakSelf.blankImage?:@"failed_to_load" title:blankTitle?:@"加载失败"]; }else{ [weakSelf showBlankWithImage:weakSelf.blankImage?:@"failed_to_load" title:blankTitle?:@"加载失败" message:weakSelf.blankMessage action:nil offsetY:weakSelf.offsetY]; } } } else { [weakSelf dismissBlank]; if(!weakSelf.allowExternalControlShowBlank){ weakSelf.mj_footer.hidden = NO; [weakSelf setHeadIsNil]; }else{ weakSelf.mj_footer.hidden = YES; } // [ShowtipTool showErrorWithStatus:blankTitle]; } }]; } //针对学习模块 - 删除所有数据之后再进行刷新 - (void)removeAllDataToReload{ self.mj_footer.hidden = YES; if(self.allowShowBlankWithHeaderTableView){ [self addHeaderView:self.blankImage?:@"no_data" title:self.blankTitle?:@"暂无数据"]; }else{ [self showBlankWithImage:self.blankImage?:@"no_data" title:self.blankTitle?:@"暂无数据" message:self.blankMessage action:nil offsetY:self.offsetY]; } } - (void)showBlankView:(NSString *)blankImg blankTitle:(NSString *)blankTitle{ [self.mj_header endRefreshing]; [self.mj_footer endRefreshing]; self.mj_footer.hidden = YES; BOOL isHaveNet = NO; if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown || AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) { isHaveNet = YES; } if (isHaveNet) { blankImg = @"no_network"; blankTitle = NET_CONNECT_ERROR; } if(self.allowShowBlankWithHeaderTableView){ [self addHeaderView:blankImg title:blankTitle]; }else{ [self showBlankWithImage:blankImg title:blankTitle message:self.blankMessage action:nil offsetY:self.offsetY]; } } -(void)addHeaderView:(NSString *)blankImg title:(NSString *)blankTitle{ if(self.allowShowBlankWithHeaderTableViewByNotDeveloped){ [self setHeadImageAndTextWithNotDeveloped:[UIImage imageNamed:@"will_open"]]; }else{ [self setHeadImageAndText:[UIImage imageNamed:blankImg] text:blankTitle]; } } #pragma mark 移除“没有数据”文字+图片 -(void)removeBlackView{ [self dismissBlank]; self.tableFooterView = nil; } #pragma mark 重新设置BlankView的位置 -(void)showBlankView_offsetY:(CGFloat)offsetY{ self.tableFooterView = nil; if(self.dataArray.count == 0){ offsetY = offsetY/2; [self dismissBlank]; UIView *fv = [[UIView alloc] init]; fv.backgroundColor = [UIColor whiteColor]; self.tableFooterView = fv; fv.height = 300; UIImageView *blankImageV = [[UIImageView alloc] init]; [fv addSubview:blankImageV]; UILabel *blankLabel = [[UILabel alloc] init]; [fv addSubview:blankLabel]; blankLabel.font = [UIFont boldSystemFontOfSize:11]; blankLabel.textColor = RGB(42, 42, 42); blankImageV.image = [UIImage imageNamed:self.blankImage?:@"no_data"]; blankLabel.text = self.blankTitle?:@"暂无数据"; [blankImageV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(fv).offset(50); make.centerX.equalTo(fv); }]; [blankLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(fv); make.bottom.equalTo(blankImageV).offset(20); }]; [self reloadData]; } } @end