-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathMRListView.m
132 lines (116 loc) · 4.63 KB
/
MRListView.m
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
//
// MRListView.m
// Coding_iOS
//
// Created by Ease on 15/10/23.
// Copyright © 2015年 Coding. All rights reserved.
//
#import "MRListView.h"
#import "ODRefreshControl.h"
#import "SVPullToRefresh.h"
#import "Coding_NetAPIManager.h"
#import "MRPRListCell.h"
@interface MRListView ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) ODRefreshControl *myRefreshControl;
@end
@implementation MRListView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
_myTableView = ({
UITableView *tableView = [[UITableView alloc] init];
tableView.backgroundColor = [UIColor clearColor];
tableView.delegate = self;
tableView.dataSource = self;
[tableView registerClass:[MRPRListCell class] forCellReuseIdentifier:kCellIdentifier_MRPRListCell];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self addSubview:tableView];
[tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 49, 0);//外部 segment bar 的高度
tableView.contentInset = insets;
tableView.scrollIndicatorInsets = insets;
tableView.estimatedRowHeight = 0;
tableView.estimatedSectionHeaderHeight = 0;
tableView.estimatedSectionFooterHeight = 0;
tableView;
});
_myRefreshControl = [[ODRefreshControl alloc] initInScrollView:self.myTableView];
[_myRefreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];
__weak typeof(self) weakSelf = self;
[_myTableView addInfiniteScrollingWithActionHandler:^{
[weakSelf refreshMore:YES];
}];
}
return self;
}
- (void)refresh{
[self refreshMore:NO];
}
- (void)refreshMore:(BOOL)willLoadMore{
MRPRS *curMRPRS = [self curMRPRS];
if (curMRPRS.isLoading) {
return;
}
if (willLoadMore && !curMRPRS.canLoadMore) {
[_myTableView.infiniteScrollingView stopAnimating];
return;
}
curMRPRS.willLoadMore = willLoadMore;
[self sendRequest:curMRPRS];
}
- (void)sendRequest:(MRPRS *)curMRPRS{
if (curMRPRS.list.count <= 0) {
[self beginLoading];
}
__weak typeof(self) weakSelf = self;
__weak typeof(curMRPRS) weakMRPRS = curMRPRS;
[[Coding_NetAPIManager sharedManager] request_MRPRS_WithObj:curMRPRS andBlock:^(MRPRS *data, NSError *error) {
[weakSelf endLoading];
[weakSelf.myRefreshControl endRefreshing];
[weakSelf.myTableView.infiniteScrollingView stopAnimating];
if (data) {
[weakMRPRS configWithMRPRS:data];
[weakSelf.myTableView reloadData];
weakSelf.myTableView.showsInfiniteScrolling = [weakSelf curMRPRS].canLoadMore;
}
[weakSelf configBlankPage:EaseBlankPageTypeView hasData:([weakSelf curMRPRS].list.count > 0) hasError:(error != nil) reloadButtonBlock:^(id sender) {
[weakSelf refreshMore:NO];
}];
}];
}
- (void)refreshToQueryData{
if (self.curMRPRS.list > 0) {
__weak typeof(self) weakSelf = self;
[self configBlankPage:EaseBlankPageTypeView hasData:([self curMRPRS].list.count > 0) hasError:NO reloadButtonBlock:^(id sender) {
[weakSelf refreshMore:NO];
}];
}
[self.myTableView reloadData];
[self refresh];
}
#pragma mark TableM
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self curMRPRS].list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MRPR *curMRPR = [[self curMRPRS].list objectAtIndex:indexPath.row];
MRPRListCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_MRPRListCell forIndexPath:indexPath];
cell.curMRPR = curMRPR;
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:60];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return [MRPRListCell cellHeightWithObj:[[self curMRPRS].list objectAtIndex:indexPath.row]];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (self.clickedMRBlock) {
MRPR *curMR = self.curMRPRS.list[indexPath.row];
self.clickedMRBlock(curMR);
}
}
@end