美文网首页
UITableView或UICollectionView滚动时显

UITableView或UICollectionView滚动时显

作者: 番薯大佬 | 来源:发表于2018-01-23 16:14 被阅读8次
页数

实现代码

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

相关文章

网友评论

      本文标题:UITableView或UICollectionView滚动时显

      本文链接:https://www.haomeiwen.com/subject/vnisaxtx.html