UITableView with custom paging-like behaviour(具有自定义分页行为的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:具有自定义分页行为的 UITableView
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01