1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
// 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 setTitle:NO_MORE_DATA forState:MJRefreshStateNoMoreData];
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