具有自定义分页行为的 UITableView

时间:2022-11-09
本文介绍了具有自定义分页行为的 UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I am trying to implement paging for custom sized pages in the UITableView. What I am trying to achieve is to have the top of the active cell align with the top of the tableView, whilst still showing the top of the next cell at the bottom of the tableView (to incline the user to scroll and see more cells).

我的单元格都等高.

如果我设置 paging=YES 这会导致轻微的偏移量随着我翻阅页面而增加.这是由于我的 tableView 比单个单元格略高,并且单元格高度/页面大小未对齐.

If I set paging=YES this results in a slight offset that increases as I flick through the pages. This is due to my tableView being slightly taller than a single cell and the cell height/page size not aligning.

我在启用分页的情况下尝试了不同的方法.我尝试将 tableView 的大小设置为单元格的高度,然后关闭剪辑和遮罩,以便用户仍然可以看到下一个单元格.这不起作用,因为下一个单元格仅在单元格滚动到 tableView 的边界框之前的最后一毫秒添加到底层滚动视图.

I have tried different things with paging enabled. I tried setting the size of the tableView to the height of the cell, but then turning off clipping and masking so the user could still see the next cell. This does not work as the next cell is only added to the underlying scrollView at the very last ms before the cell scrolls into the bounding box of the tableView.

然后我开始实现不同的 scrollView 委托方法来模拟分页行为 - 我似乎无法做到这一点.

I then started implementing the different scrollView delegate methods to mimic the paging behaviour - I can not seem to get it right.

除其他外,我尝试过这样的事情:

I have, among other things, tried something like this:

- (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 完全不同.

While it sort of does the right thing, it behaves nothing like a scrollView/tableView with paging enabled.

我在这里找到了一些试图实现相同目标的人的帖子,但答案与我自己尝试过的任何事情一样具有非本地快照感觉".

I have found a few posts here from people trying to achieve the same thing, but the answers suffer from the same "non-native-snap-feel" that anything I tried myself does.

感谢您提供的任何帮助.

Thanks for any help given.

iOS >= 5.0

推荐答案

实现 scrollViewWillEndDragging:withVelocity:targetContentOffset: 以返回距离 targetContentOffset 最近的单元格的顶部坐标,而不是最接近您的起始偏移量.jjv360 提供的解决方案可以很好地做到这一点,但您可能需要根据单元格的平均高度对其进行一些调整(jjv360 的解决方案可能对非常大的单元格过于活泼).

Implement scrollViewWillEndDragging:withVelocity:targetContentOffset: to return top coordinate of the cell closest to targetContentOffset, not closest to your starting offset. The solution jjv360 provided does that well, but you might want to tweak it a bit depending on how high your cells are on average (jjv360's solution might be too snappy on really big cells).

我想补充一点,您可以通过更改其 decelerationRate 属性来使 UITableView 的减速更快,使其表现得更像分页 UIScrollView(只需在 init/viewDidLoad/wherever 中执行一次).

I would just like to add that you can make UITableView's deceleration faster to behave more like paged UIScrollView by changing its decelerationRate property (just do it once in init/viewDidLoad/wherever).

self.tableView.decelerationRate = UIScrollViewDecelerationRateFast;

这篇关于具有自定义分页行为的 UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:NSURLRequest 在 UIScrollView 滚动时不会触发 下一篇:以编程方式将 UIScrollView 滚动到 Swift 中的子 UIView(子视图)的顶部

相关文章

最新文章