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;}




0 0
原创粉丝点击