计算UITableView在哪一行停止滚动?
当表格有很多行时,用户可以向上/向下滑动表格。这将创建一个滚动动画,该动画似乎具有确定的长度,具体取决于轻弹手势的速度/长度。如果没有进一步的用户交互,是否可以可靠地计算一旦滚动停止,表中的哪些行将可见?
When a table has many rows, the user can flick up/down the table. This creates a scrolling animation which seems to have a deterministic length depending on the speed/length of the flick gesture. Is it possible to reliably calculate what rows in the table will be visible once the scrolling stops if there is no further user interaction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 iOS 5.0 及更高版本中,有一个针对 UIScrollViewDelegate 称为 scrollViewWillEndDragging:withVelocity:targetContentOffset:。使用这种新方法,可以计算甚至修改滚动的停止位置,如视频会话 100:新增内容中所述在 Cocoa Touch 中,大约第九分钟。
In iOS 5.0 and later there is a new API for the UIScrollViewDelegate called scrollViewWillEndDragging:withVelocity:targetContentOffset:. With this new method the stop position of the scroll can be calculated and even modified, as explained in the Video Session 100: What's New in Cocoa Touch, around the ninth minute.
UITableView
继承自UIScrollView
,您可以通过使用UIScrollViewDelegate
方法和表视图indexPathsForVisibleRows
属性来检查来完成此操作滚动停止时哪些单元格索引路径可见。您甚至可以保存从减速开始的初始位置,以便您可以计算滚动方向是向上还是向下,然后让您知道它将停止的单元格是可见的第一个还是最后一个那些。
关于上面代码的一个重要注意事项是,它将表格视图作为变量
_myTableview
指向,而不是仅仅将委托方法变量scrollView
转换为UITableView *< /code>,尽管这只是实现细节,不应该影响这里的逻辑。
UITableView
inherits fromUIScrollView
and you can accomplish that by using theUIScrollViewDelegate
methods and the table viewindexPathsForVisibleRows
property to check which cell index paths are visible at the moment the scrolling stops.You can even save the initial position from where the deceleration started, so that you can calculate whether the scroll direction was up or down, which will then let you know if is the cell that it will stop is the first or the last of the visible ones.
An important note about the code above is that it points to the table view as a variable
_myTableview
instead of just casting the delegate method variablescrollView
to aUITableView *
, although that is just implementation details and should not affect the logic here.有趣的问题......
UITableViewDelegate
也符合UIScrollViewDelegate
:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScrollViewDelegate_Protocol/Reference/UIScrollViewDelegate.html#//apple_ref/occ/intf/UIScrollViewDelegate有您可以使用一些委托回调来了解滚动何时开始减速以及何时结束减速。
您可能可以使用
–scrollViewDidEndDecelerating:
,然后使用 tableView(tableView 子类UIScrollView
)的单元格高度和内容偏移属性,然后计算可见的单元格减速后。Interesting question.....
UITableViewDelegate
conforms toUIScrollViewDelegate
as well: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScrollViewDelegate_Protocol/Reference/UIScrollViewDelegate.html#//apple_ref/occ/intf/UIScrollViewDelegateThere are some delegate callbacks you could use to know when the scrolling begins to decelerate, and ends decelerating.
You could probably use
– scrollViewDidEndDecelerating:
and at that point use the cell heights and the content offset property of the tableView (tableView subclassesUIScrollView
) and then calculate the cells that are visible after deceleration.我不知道如何确定将显示多少行,但您始终可以获得已显示的行数。 (一旦桌子停止,不再接触。)
不确定这是否有帮助,但这就是你要做的
I dont know how you could determine how many rows WILL show, but you can always get how many rows HAVE shown. (once the table stops w/no further touching.)
Not sure if that helps but this is how you would do it