123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486 |
- //
- // 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 <Masonry.h>
- #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<GHRefreshTableViewDelegate>)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
|