CNLiveCollectionController.m
8.24 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
// CNLiveCollectionController.m
// CNLiveCollectionModule
//
// Created by 153993236@qq.com on 11/12/2019.
// Copyright (c) 2019 153993236@qq.com. All rights reserved.
//
#import "CNLiveCollectionController.h"
#import "QMUIKit.h"
#import "CNLiveCategory.h"
#import "CNLiveRefreshHeader.h"
#import "CNLiveRrfreshFooter.h"
#import "CNLiveCollectionNavigationBar.h"
#import "CNLiveCollectionCell.h"
#import "CNLiveCollectionModel.h"
#import "CNLiveCollectionRequest.h"
@interface CNLiveCollectionController ()<UITableViewDelegate, UITableViewDataSource,CNLiveCollectionNavigationBarDelegate,CNLiveCollectionCellDelegate>
@property (nonatomic, strong) CNLiveCollectionNavigationBar *navigationBar;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *data;
@property (nonatomic, assign) NSInteger pageNo;
@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, assign) BOOL isRefreshing;
@end
@implementation CNLiveCollectionController
static const CGFloat timeValue = 1.5;
#pragma mark - 数据
- (void)loadData{
[QMUITips showLoadingInView:self.view];
__weak typeof(self) weakSelf = self;
[CNLiveCollectionRequest getCollectionList:[NSString stringWithFormat:@"%ld",_pageNo] pageSize:[NSString stringWithFormat:@"%ld",_pageSize] block:^(BOOL isSuccess, NSArray * _Nonnull array, NSError * _Nonnull error) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
[QMUITips hideAllTipsInView:strongSelf.view];
[strongSelf hideEmptyView];
[strongSelf.tableView.mj_header endRefreshing];
[strongSelf.tableView.mj_footer endRefreshing];
if (error) {
if (strongSelf.data.count == 0) {
if (error.code == -1001||error.code == -1003||error.code == -1009) {
[strongSelf showEmptyViewWithImage:[UIImage imageNamed:@"dd_wlcw"] text:@"无法连接网络" detailText:@"别紧张,试试看刷新页面~" buttonTitle:@" 重试 " buttonAction:@selector(loadData)];
[strongSelf setCNAPIErrorEmptyView];
strongSelf.tableView.mj_footer.hidden = strongSelf.data.count == 0;
}
}
return;
}
if (isSuccess) {
if(strongSelf.pageNo == 1 && strongSelf.data.count > 0){
[strongSelf.data removeAllObjects];
}
strongSelf.isRefreshing = NO;
strongSelf.pageNo++;
[strongSelf.data addObjectsFromArray:array];
if (array.count < strongSelf.pageSize) {
[strongSelf.tableView.mj_footer endRefreshingWithNoMoreData];
} else {
[strongSelf.tableView.mj_footer endRefreshing];
}
}
if (strongSelf.data.count == 0) {//显示empty
[strongSelf showEmptyViewWithImage:[UIImage imageNamed:@"zhuangtai_kong"] text:@"您现在没有相关内容哦" detailText:@"" buttonTitle:@"" buttonAction:nil];
[strongSelf setCNNoDataEmptyView];
strongSelf.tableView.tableFooterView = nil;
}
strongSelf.tableView.mj_footer.hidden = self.data.count == 0;
[strongSelf.tableView reloadData];
}];
}
- (void)loadMoreData{
if(!_isRefreshing){
[self loadData];
}
}
#pragma mark - 生命周期
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
if (@available(iOS 11, *)) {
}
else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
_isRefreshing = NO;
_pageNo = 1;
_pageSize = 10;
[self loadData];
[self createSubViews];
}
- (void)dealloc {
NSLog(@"CNLiveCollectionController -- dealloc");
}
- (void)createSubViews {
[self.view addSubview:self.navigationBar];
[self.view addSubview:self.tableView];
}
#pragma mark - 响应方法
- (void)goBack {
if (self.presentingViewController != nil) {
[self dismissViewControllerAnimated:YES completion:nil];
}
else {
[self.navigationController popViewControllerAnimated:YES];
}
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CNLiveCollectionModel *model = self.data[indexPath.row];
return model.height;
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CNLiveCollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:kCNLiveCollectionCell];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
CNLiveCollectionModel *model = self.data[indexPath.row];
cell.model = model;
cell.delegate = self;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
#pragma mark - CNLiveCollectionCellDelegate
- (void)cancelCollection:(CNLiveCollectionCell *)cell{
[QMUITips showLoadingInView:self.view];
__weak typeof(self) weakSelf = self;
[CNLiveCollectionRequest cancelCollection:cell.model.collectionId block:^(BOOL isSuccess, NSError * _Nonnull error) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if(!strongSelf) return ;
[QMUITips hideAllToastInView:strongSelf.view animated:YES];
if (error) {
[QMUITips showError:error.localizedDescription inView:strongSelf.view hideAfterDelay:timeValue];
return;
}
if (isSuccess) {
[strongSelf.data removeObject:cell.model];
[strongSelf.tableView reloadData];
[QMUITips showSucceed:@"取消收藏成功" inView:strongSelf.view hideAfterDelay:timeValue];
if (strongSelf.data.count == 0) {
[strongSelf showEmptyViewWithImage:[UIImage imageNamed:@"zhuangtai_kong"] text:@"您现在没有相关内容哦" detailText:@"" buttonTitle:@"" buttonAction:nil];
[strongSelf setCNNoDataEmptyView];
strongSelf.tableView.tableFooterView = nil;
}
strongSelf.tableView.mj_footer.hidden = strongSelf.data.count == 0;
}
}];
}
#pragma mark - CNLiveCollectionNavigationBarDelegate
- (void)navigationBar:(CNLiveCollectionNavigationBar *)navigationBar actionEvents:(NSString *)actionEvents{
if ([actionEvents isEqualToString:@"1"]) {
[self goBack];
}
}
#pragma mark - 懒加载
- (NSMutableArray *)data {
if (!_data) {
_data = [NSMutableArray array];
}
return _data;
}
- (UITableView *)tableView{
if(!_tableView){
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavigationContentTop, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationContentTop) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0];
_tableView.estimatedRowHeight = 0;
_tableView.estimatedSectionHeaderHeight = 0;
_tableView.estimatedSectionFooterHeight = 0;
[_tableView registerClass:[CNLiveCollectionCell class] forCellReuseIdentifier:kCNLiveCollectionCell];
_tableView.mj_header = [CNLiveRefreshHeader headerWithRefreshingBlock:^{
self.pageNo = 1;
[self loadData];
}];
CNLiveRrfreshFooter *footer = [CNLiveRrfreshFooter footerWithRefreshingBlock:^{
[self loadMoreData];
}];
footer.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0];
_tableView.mj_footer = footer;
}
return _tableView;
}
- (CNLiveCollectionNavigationBar *)navigationBar {
if (!_navigationBar) {
_navigationBar = [[CNLiveCollectionNavigationBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, NavigationContentTop)];
_navigationBar.delegate = self;
_navigationBar.title.text = @"我的收藏";
}
return _navigationBar;
}
@end