UITableView拖动时计算页码 & 往上拖拉时自动加载
来源:互联网 发布:查询数据库所有触发器 编辑:程序博客网 时间:2024/05/24 06:25
在电商平台中,使用UITableView展示商品信息时,会出现需要显示页码的情况,也会出现需要在向上拖动列表到某个范围时,自动发送请求加载更多商品的情况。
Demo示例
实现效果图如下所示:
实现原理
1、页码
(1)已知显示页面的宽和高,取高heightTotal
(2)已知UITableViewCell单元格的宽和高,取高heightCell
(3)UITableView继承UIScrollView,拖动时响应滚动代理方法,并获取所有可见的cell数组,取最后一个数组中的最后一个cell
(4)每个cell在UITableView中的frame是不同的,取frame中的y
(5)已知商品信息的总数countTotal
(6)计算页面可显示的个数,pageSize = heightTotal / heightCell
(7)计算页面总数,pageTotal = countTotal / pageSize
(8)计算当前页数
(8-1)计算当前cell个数,countCurrent = y / heightCell + (y % heightCell == 0 ? 0 : 1)
(8-2)计算当前页码数,pageCurrent = countCurrent / pageSize + (countCurrent % pageSize == 0 ? 0 : 1)
(9)代码示例
// cell高度static CGFloat const heightCell = 100.0;
// 视图显示多少个cell- (NSInteger)pageSize{ NSInteger size = (NSInteger)(self.mainTableView.frame.size.height / heightCell); return size;}
// 总页码- (NSInteger)pageTotal{ NSInteger total = self.mainArray.count / self.pageSize + (self.mainArray.count % self.pageSize == 0 ? 0 : 1); return total;}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ NSArray *array = [self.mainTableView visibleCells];// NSLog(@"1 %s array = %@", __func__, array); if (array && 0 < array.count) { // 计算页码 UITableViewCell *cell = array.lastObject; NSInteger heightCurrent = cell.frame.origin.y; NSInteger height = cell.frame.size.height; NSInteger count = heightCurrent / height + (heightCurrent % height == 0 ? 0 : 1); NSInteger pageCurrent = count / self.pageSize + (count % self.pageSize == 0 ? 0 : 1); NSString *page = [NSString stringWithFormat:@"%@/%@", @(pageCurrent), @(self.pageTotal)]; self.pageLabel.text = page; }}
2、拖动加载
(1)在计算页码的基础上实现
(2)定义前一个停止拖动时的位移,取previousY
(3)获取拖动位移,取currentY = scrollView.contentOffset.y
(4)判断是向上还是向下拖动,取isUpDrag = (currentY - previousY > 0 ? YES :NO)
(5)设置前一个停止拖动时的位移值,取previousY = currentY
(6)设置当前页面拖动到还剩下两个单元格范围时,且是向上拖动时,自动加载更多,取(countCurrent % pageSize == 2 && isUpDrag)
(7)代码示例
CGFloat previousY = 0.0;- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ NSArray *array = [self.mainTableView visibleCells];// NSLog(@"1 %s array = %@", __func__, array); if (array && 0 < array.count) { // 计算页码 UITableViewCell *cell = array.lastObject; NSInteger heightCurrent = cell.frame.origin.y; NSInteger height = cell.frame.size.height; NSInteger count = heightCurrent / height + (heightCurrent % height == 0 ? 0 : 1); NSInteger pageCurrent = count / self.pageSize + (count % self.pageSize == 0 ? 0 : 1); NSString *page = [NSString stringWithFormat:@"%@/%@", @(pageCurrent), @(self.pageTotal)]; self.pageLabel.text = page; CGFloat offsetY = scrollView.contentOffset.y; BOOL isScrollUp = (offsetY - previousY > 0.0 ? YES : NO); previousY = offsetY; NSLog(@"offsetY = %@", @(offsetY)); // 向上拖动时,自动加载更多 if (count % self.pageSize == 2 && isScrollUp) { NSLog(@"index = %@, loading...", @(count)); if (self.mainArray.count < countMax) { [self loadMoreData]; } } }}
另外,需要注意其他计算情况。
情况1:指定每页的大小为N,当N = 10时;代码示例如下:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ NSArray *array = [self visibleCells]; if (array && 0 < array.count) { // 计算页码 VSTGoodsListCell *cell = array.lastObject; NSInteger heightCurrent = cell.frame.origin.y; // cell高度计算页码大小// NSInteger height = cell.frame.size.height;// NSInteger count = heightCurrent / height + (heightCurrent % height == 0 ? 0 : 1);// pageCurrent = count / self.pageSize + (count % self.pageSize == 0 ? 0 : 1); // 固定个数为页码大小 NSInteger height = heightVSTGoodsListCell * self.pageSize; NSInteger count = heightCurrent / height + (heightCurrent % height == 0 ? 0 : 1); pageCurrent = count; // 向上拖动时,自动加载更多 isStart = NO; CGFloat offsetY = scrollView.contentOffset.y; BOOL isScrollUp = (offsetY - previousY > 0.0 ? YES : NO); previousY = offsetY; if (count % self.pageSize == 2 && isScrollUp) { if (self.dataArray.count < self.countTotal) { // doing... } } }}#pragma mark - getter// 视图显示多少个cell- (NSInteger)pageSize{ // cell高度计算页码大小// NSInteger size = (NSInteger)(self.frame.size.height / heightVSTGoodsListCell);// return size; // 固定个数为页码大小 return kPageSize;}// 总页码- (NSInteger)pageTotal{ NSInteger total = self.countTotal / self.pageSize + (self.countTotal % self.pageSize == 0 ? 0 : 1); return total;}
情况2:在collection中计算时,计算方法与在table中计算方法有所区别,代码示例如下:
考虑1:每行显示多少个cell
考虑2:获取当前最大的偏移量值最大的cell进行计算
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ NSArray *array = [self visibleCells]; NSLog(@"size = %@, cells = %@", NSStringFromCGSize(self.contentSize), array); if (array && 0 < array.count) { // 计算页码 CGFloat maxY = 0.0; VSTGoodsListCollectionCell *cell = nil; for (VSTGoodsListCollectionCell *subCell in array) { CGFloat originY = subCell.frame.origin.y; if (maxY < originY) { maxY = originY; cell = subCell; } } NSInteger heightCurrent = cell.frame.origin.y; // cell高度计算页码大小// NSInteger height = cell.frame.size.height;// NSInteger count = heightCurrent / height + (heightCurrent % height == 0 ? 0 : 1);// pageCurrent = count / self.pageSize + (count % self.pageSize == 0 ? 0 : 1); // 固定个数为页码大小 NSInteger height = heightVSTGoodsListCollectionCell * (self.pageSize / 2); NSInteger count = heightCurrent / height + (heightCurrent % height == 0 ? 0 : 1); pageCurrent = count; // 向上拖动时,自动加载更多 isStart = NO; CGFloat offsetY = scrollView.contentOffset.y; BOOL isScrollUp = (offsetY - previousY > 0.0 ? YES : NO); previousY = offsetY; if (count % self.pageSize == 2 && isScrollUp) { if (self.dataArray.count < self.countTotal) { // doing... } } }}// 视图显示多少个cell- (NSInteger)pageSize{ // cell高度计算页码大小// NSInteger size = (NSInteger)(self.frame.size.height / heightVSTGoodsListCollectionCell);// return size; // 固定个数为页码大小 return kPageSize;}// 总页码- (NSInteger)pageTotal{ NSInteger total = self.countTotal / self.pageSize + (self.countTotal % self.pageSize == 0 ? 0 : 1); return total;}
- UITableView拖动时计算页码 & 往上拖拉时自动加载
- 关于上拉刷新控件MJRefresh 上拉时当加载出来后仍会自动往上滑动的解决
- UITableView 滑动自动加载更多数据
- 数据多自动往上滚动
- 可拖动,可拖拉大小的层
- PDF自动生成页码
- 自动生成页码
- 带延时加载,自动刷新和拖动控制的Canvas
- 带延时加载,自动刷新和拖动控制的Canvas
- 修改MTCNN中caffe源码,时计算loss时以对应标签是否为“-1”判别
- 修改MTCNN中caffe源码,时计算loss时以对应标签是否为“-1”判别
- 云风:让 lua 编译时计算
- adobe安装时计算机挂起解决办法
- Lotus 域类型(可编辑,计算,创建时计算、显示时计算)
- Lotus 域类型(可编辑,计算,创建时计算、显示时计算)
- IOS UITableView拖动排序功能
- 用三个独立按键实现按下第一个时计时停止,按下第二个时计时开始,按下第三个时计数值清零从头开始,秒表设计!!!!
- Android 当ScrollView里面嵌套Listview时计算高度
- 解决MDK5在调试中崩溃,提示“IDE已停止工作”的一种方法
- Java双缓冲队列实现
- Android异步下载图片并且缓存图片到本地
- Handler
- ICE的使用
- UITableView拖动时计算页码 & 往上拖拉时自动加载
- MySQL提示:The server quit without updating PID file问题的解决办法
- nyoj 104最大和
- 调用Metasploit RestFul接口,解决证书问题
- linux下查看mysql启动状态
- 条件熵的定义
- kibana插件开发
- HBase中WAL(Write-Ahead-Log)的特性与场景分析
- jquery validate 详解一