UITableView或UICollectionView滚动时显示页数

页数

实现代码

// UITableView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSArray *array = [self visibleCells];
    if (array && 0 < array.count)
    {
        // 计算页码
        VSTGoodsListCell *cell = array.lastObject;
        NSInteger heightCurrent = (cell.frame.origin.y + cell.frame.size.height);
        // 方法1 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);
        // 方法2 固定个数为页码大小
        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)
            {
                isStart = YES;
            }
        }
        
        if (self.scrollBlock)
        {
            self.scrollBlock(pageCurrent, self.pageTotal, isShow, isStart);
        }
    }
}
// UICollectionView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSArray *array = [self visibleCells];
    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.frame.size.height);
        // 方法1 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);
        // 方法2 固定个数为页码大小
        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)
            {
                isStart = YES;
            }
        }

        if (self.scrollBlock)
        {
            self.scrollBlock(pageCurrent, self.pageTotal, isShow, isStart);
        }
    }
}
// 视图显示多少个cell
- (NSInteger)pageSize
{
    // 方法1 cell高度计算页码大小
//    NSInteger size = (NSInteger)(self.frame.size.height / heightVSTGoodsListCell);
//    return size;
    
    // 方法2 固定个数为页码大小,如:每页个数
    return kPageSize;
}

// 总页码,如:countTotal为总个数
- (NSInteger)pageTotal
{
    NSInteger total = self.countTotal / self.pageSize + (self.countTotal % self.pageSize == 0 ? 0 : 1);
    return total;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容