// // KWMCollectionRefreshUtil.m // iCemarose // // Created by HouWeiBin on 2017/6/16. // Copyright © 2017年 kollway. All rights reserved. // #import "KWMCollectionRefreshUtil.h" #import "KWMLoadingHeader.h" #import "MJRefresh.h" #import "KWMStringUtil.h" #import "UIColor+SAMAdditions.h" @interface KWMCollectionRefreshUtil() @end @implementation KWMCollectionRefreshUtil -(NSMutableArray *)dataList{ if(!_dataList){ _dataList = [NSMutableArray new]; } if (_dataList.count > 0) { if (self.dataFilter) { NSMutableArray *filted = [NSMutableArray new]; for (id data in _dataList) { if (self.dataFilter(data)) { [filted addObject:data]; } } return filted; } } return _dataList; } - (BOOL)isLoading { return [self.collectionView.mj_header isRefreshing] || [self.collectionView.mj_footer isRefreshing]; } - (void)hideLoading { [self.collectionView.mj_header endRefreshing]; [self.collectionView.mj_footer endRefreshing]; }; //隐藏上啦下啦刷新 -(void)setRefreshNull{ [self.collectionView setMj_header:nil]; [self.collectionView setMj_footer:nil]; } - (void)appendDataList:(NSArray *)result { if (self.page == 1) { [self.dataList removeAllObjects]; } if (result) { [self.dataList addObjectsFromArray:result]; } [self.collectionView reloadData]; BOOL hide = self.dataList.count > 0; self.emptyView.hidden = hide; self.lastResult = [NSMutableArray arrayWithArray:result]; } - (UIView *)emptyView { if (_emptyView == nil) { UIView *emptyView = [[UIView alloc] init]; emptyView.backgroundColor = [UIColor clearColor]; emptyView.frame = CGRectMake(0, 0, CGRectGetWidth(self.collectionView.frame), CGRectGetHeight(self.collectionView.frame) ); NSString *msg = @"暂时没有数据"; if (![KWMStringUtil isEmpty:self.emptyMsg]) { msg = self.emptyMsg; } if(self.isShowEmptyImage){ emptyView.backgroundColor = [UIColor whiteColor]; UIView *childView = [self getHasImageView:emptyView setTips:msg]; [emptyView addSubview:childView]; }else{ CGFloat labelHeight = 25; NSInteger labelY = CGRectGetHeight(emptyView.frame)/2 - labelHeight; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, labelY, CGRectGetWidth(emptyView.frame), labelHeight)]; label.textAlignment = NSTextAlignmentCenter; label.textColor = [UIColor darkGrayColor]; label.font = [UIFont systemFontOfSize:14.0f]; label.text = msg; [emptyView addSubview:label]; } [self.collectionView addSubview:emptyView]; [self.collectionView bringSubviewToFront:emptyView]; _emptyView = emptyView; } return _emptyView; } //有图片的提示view -(UIView *)getHasImageView:(UIView *)emptyView setTips:(NSString *)tips{ NSInteger childViewY = CGRectGetHeight(emptyView.frame)/2-130;//130是空view高度的一半 NSInteger childViewX = CGRectGetWidth(emptyView.frame)/2-100;//100是空view宽度的一半 UIView *childView = [[UIView alloc] initWithFrame:CGRectMake(childViewX, childViewY, 200, 260)]; childView.backgroundColor = [UIColor clearColor]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(70, 100, 60, 60)];//70是空view的宽减去image的宽除2(100也是同理) imageView.clipsToBounds = YES; [imageView setImage:[UIImage imageNamed:self.imageName]]; imageView.contentMode = UIViewContentModeScaleAspectFit; [childView addSubview:imageView]; UILabel *lbFirst = [[UILabel alloc] initWithFrame:CGRectMake(0, 170,200, 20)]; lbFirst.textAlignment = NSTextAlignmentCenter; lbFirst.textColor = [UIColor sam_colorWithHex:@"EAECEE"]; lbFirst.font = [UIFont systemFontOfSize:14.0f]; lbFirst.text = tips; [childView addSubview:lbFirst]; return childView; } -(BOOL)isReload{ return self.page == 1; } -(void)setHasNextPage:(BOOL)hasNextPage{ _hasNextPage = hasNextPage; self.collectionView.mj_footer.hidden = !hasNextPage; } -(void)setCollectionView:(UICollectionView *)collectionView{ _collectionView = collectionView; if (!self.hideHeader || !collectionView.mj_header) { collectionView.mj_header = [self createHeader]; } if (collectionView.mj_footer == nil) { collectionView.mj_footer = [self createFooter]; } } -(MJRefreshHeader *)createHeader{ __weak KWMCollectionRefreshUtil *weakSelf = self; KWMLoadingHeader *header = [KWMLoadingHeader headerWithRefreshingBlock:^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [weakSelf reLoadData]; }); }]; return header; } - (MJRefreshAutoNormalFooter *)createFooter { __weak KWMCollectionRefreshUtil *weakSelf = self; MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [weakSelf loadData]; }); }]; footer.hidden = YES; return footer; } - (void)reLoadData { // self.page = 0; // [self loadData]; [self.delegate refreshUtil:self onLoad:0]; } -(void)clearData{ self.lastResult = nil; [self.dataList removeAllObjects]; [self.collectionView reloadData]; } - (void)loadData { [self.delegate refreshUtil:self onLoad:1]; } @end