具有自定义分页行为的UITableView

时间:2012-09-04 21:31:04

标签: ios uitableview uiscrollview

我正在尝试在UITableView中实现自定义大小页面的分页。 我想要实现的是让活动单元格的顶部与 tableView的顶部,同时仍然显示tableView底部的下一个单元格的顶部(以倾斜用户滚动并查看更多单元格)。

我的细胞高度相等。

如果我设置paging=YES,这会导致稍微偏移,当我浏览页面时会增加。这是因为我的tableView比单个单元格略高,并且单元格高度/页面大小没有对齐。

我尝试过启用分页的不同功能。我尝试将tableView的大小设置为单元格的高度,但随后关闭剪切和屏蔽,以便用户仍然可以看到下一个单元格。这不起作用,因为下一个单元格仅在单元格滚动到tableView的边界框之前的最后一个ms添加到底层的scrollView。

然后我开始实现不同的scrollView委托方法来模仿分页行为 - 我似乎无法做到正确。

除此之外,我还尝试过这样的事情:

- (void) scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    float cellHeight = [myCell rowHeight];
    int index = floorf(scrollView.contentOffset.y / cellHeight);

    *targetContentOffset = CGPointMake(targetContentOffset->x, targetContentOffset->y = (index * cellHeight));
}

虽然它做了正确的事情,但它的行为与启用了分页的scrollView / tableView完全不同。

我在这里发现了一些试图达到同样效果的人的帖子,但答案却遭遇了与我自己尝试的任何事情相同的“非本地快照”。

感谢您给予的任何帮助。

iOS> = 5.0

3 个答案:

答案 0 :(得分:11)

实施scrollViewWillEndDragging:withVelocity:targetContentOffset:以返回距离targetContentOffset最近的单元格的顶部坐标,而不是最接近您的起始偏移量。提供的解决方案jjv360做得很好,但你可能想稍微调整它,具体取决于你的单元平均有多高(jjv360的解决方案可能在真正的大单元上太过敏捷)。

我想补充一点,你可以通过更改UITableView属性(只需在init / viewDidLoad中执行一次),使UIScrollView的减速速度更快,使其更像页面decelerationRate /哪里)。

self.tableView.decelerationRate = UIScrollViewDecelerationRateFast;

答案 1 :(得分:3)

如果关闭分页,可以使用UIScrollView委托功能:

-(void)scrollViewWillEndDragging:(UIScrollView*)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint*)targetContentOffset {

    // Get index path for target row
    NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:(*targetContentOffset)];

    // Set new target
    (*targetContentOffset) = [self.tableView rectForRowAtIndexPath:indexPath].origin;

}

答案 2 :(得分:1)

我用这段代码解决了这个问题:

- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    CGFloat pageWidth = self.collectionView.frame.size.width + 10 /* Optional Photo app like gap between images */;

    _currentPage = floor((self.collectionView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

    NSLog(@"Dragging - You are now on page %i", _currentPage);
}

-(void) scrollViewWillEndDragging:(UIScrollView*)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint*)targetContentOffset {

    CGFloat pageWidth = self.collectionView.frame.size.width + 10;

    int newPage = _currentPage;

    if (velocity.x == 0) // slow dragging not lifting finger
    {
        newPage = floor((targetContentOffset->x - pageWidth / 2) / pageWidth) + 1;
    }
    else
    {
        newPage = velocity.x > 0 ? _currentPage + 1 : _currentPage - 1;

        if (newPage < 0)
            newPage = 0;
        if (newPage > self.collectionView.contentSize.width / pageWidth)
            newPage = ceil(self.collectionView.contentSize.width / pageWidth) - 1.0;
    }

    NSLog(@"Dragging - You will be on %i page (from page %i)", newPage, _currentPage);

    *targetContentOffset = CGPointMake(newPage * pageWidth, targetContentOffset->y);
}

感谢http://www.mysamplecode.com/2012/12/ios-scrollview-example-with-paging.html以正确的方式指出。