Because the project will use the pull-up and pull-down functions of TableView and CollectionView, but does not want to refer to a lot of SDKs from three parties, it imitates its functions and makes a simple package.
The following mainly introduces the pull-up and pull-down implementation of TableView. The implementation of CollectionView is exactly the same as that of TableView.
principle:
Monitor the change of the property contentOffset in the service class UIScrollView of TableView and CollectionView, and make judgments based on the value of contentOffset.
Core code:
[self addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
//监听UITableview的顶部和头部下拉事件
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
if ([keyPath isEqualToString:@"contentOffset"] && !self.isLoading && !self.isRefreshing) {
self.refreshHeaderView.frame = CGRectMake(0, -self.changeTopHeight, self.frame.size.width, self.changeTopHeight);
self.loadingFooterView.frame = CGRectMake(0, self.contentSize.height, self.frame.size.width, self.changeFootHeight);
float height = self.contentSize.height > self.frame.size.height ?self.frame.size.height : self.contentSize.height;
if (self.isDragging) {
if (- self.contentOffset.y > self.changeTopHeight && self.didBeginRefreshBlock) {
// 调用下拉刷新方法
self.canRefech = YES;
[self.refreshHeaderView setRefreshMode:2];
}else{
self.canRefech = NO;
[self.refreshHeaderView setRefreshMode:-1];
}
if(self.isNoData && self.didBeginLoadingBlock){
self.canLoding = NO;
[self.loadingFooterView setLoadingMode:3];
}else{
if ((height - self.contentSize.height + self.contentOffset.y) > self.changeFootHeight && self.didBeginLoadingBlock) {
// 调用上拉加载方法
self.canLoding = YES;
[self.loadingFooterView setLoadingMode:4];
}else{
self.canLoding = NO;
[self.loadingFooterView setLoadingMode:-1];
}
}
}
else
{
if ((- self.contentOffset.y > self.changeTopHeight) && self.canRefech && self.didBeginRefreshBlock)
{
self.drageMode = 1;
self.canRefech = NO;
[self beginRefresh];
}
else if (((height - self.contentSize.height + self.contentOffset.y) > self.changeFootHeight) && self.canLoding && self.didBeginLoadingBlock)
{
self.drageMode = 2;
self.canLoding = NO;
if(!self.isNoData){
[self beginLoading];
}
}
}
}
}
Because it is a monitoring judgment for UIScrollView, this method is common for TableView and CollectionView and will not affect its original functions.
use:
initialization:
The initialization is no different from the normal TableView. When you need the pull-up and pull-down function, add the callback of the pull-up and pull-down event. If you don’t add the corresponding callback, the TableView will not trigger the corresponding event.
MyTableViewLoadingInfo * showInfo = [[MyTableViewLoadingInfo alloc]initWithbeginLoadingText:@"加载中" willLodingText:@"松开加载" endLoadingText:@"加载完毕" noDayaText:@"暂无更多数据" beginRefreshText:@"下拉刷新" willRefreshText:@"松开刷新" endRefreshText:@"正在刷新"];//自定义提示信息
MyTableView * tableView = [[MyTableView alloc]initWithFrame:CGRectMake(0, 0, 100, 200) style:UITableViewStylePlain showInfo:showInfo];
//MyTableView * tableView = [[MyTableView alloc]initWithFrame:CGRectMake(0, 0, 100, 200) style:UITableViewStylePlain];//使用默认提示
tableView.delegate = self;
tableView.dataSource = self;
[tableView whenBeginRefresh:^{
NSLog(@"开始刷新");
}];
[tableView whenBeginLoading:^{
NSLog(@"开始加载");
}];
End operation:
When the pull-up and pull-down are triggered, the animation will be displayed automatically. When the data request is completed, the TableView can be notified to end the operation.
//结束加载动画
[tableView endLoading];
//结束刷新动画
[tableView endRefresh];
//结束加载动画,并展示无更多数据(此后无法触发加载事件,重新刷新后可触发)
[tableView nodata];
Pagination function:
Generally, paging is involved in pulling up and down. In order to facilitate the paging function, I added some attributes to the encapsulated TableView.
//MyTableView
@property(nonatomic,readwrite)int pageIndex;//当前页码 默认0
@property(nonatomic,readwrite)int pageSize;//页码大小 默认10
//MyTableView
//使用
-(void)loadNetData:(BOOL)isRefrsh {
if(isRefrsh){
self.tableView.pageIndex = 1;
[self.datas removeAllObjects];
}else{
self.tableView.pageIndex ++;
}
[NetWorkManager SearchListPageNo:self.tableView.pageIndex pageSize:self.tableView.pageSize succeedBlock:^(ListPostRes * _Nonnull res) {
[self.datas addObjectsFromArray:res.postInfoArray];
if(isRefrsh){
[self.tableView endRefresh];
}
if(res.total <= self.datas.count){
[self.tableView nodata];
}else if(!isRefrsh){
[self.tableView endLoading];
}
[self.tableView reloadData];
} failBlock:^(NSString * _Nonnull errorstr) {
[self.tableView endRefresh];
[self.tableView endLoading];
}];
}
function trigger boundary height value
Considering that the trigger height values of the general TableView pull-up and pull-down are different, two properties are defined here
@property(nonatomic, assign)CGFloat changeTopHeight;//偏移多少触发下拉刷新 默认80
@property(nonatomic, assign)CGFloat changeFootHeight;//偏移多少触发上拉加载 默认60
download
The following is my packaged file, you can refer to
Simple encapsulation of iOS TableView pull-up and pull-down
contact author
Looking forward to your likes and attention! In case of doubt, contact the author.