滚动后检测UITableView中的当前顶部单元格 [英] Detect the current top cell in a UITableView after scrolling
问题描述
一个简单的问题,但我似乎没有正确的术语来搜索Stackoverflow。
a simple question but I don't seem to have the right terminology to search Stackoverflow by it.
我有一个没有任何部分的UITableView,用户可以滚动向上和向下查看此tableview中显示的一长串数据(行)。
I have a UITableView with no sections, the user can scroll up and down a long list of data (rows) presented inside this tableview.
问题:如何检测最顶部的单元格行号用户滚动后
(例如,如果用户向下滚动30个单元格,滚动后的顶部单元格= 30)
Question: how can I detect the most top cell row number after the user scrolled. (for example, if the user is scrolling 30 cells down, the top cell after the scroll is = 30)
推荐答案
您可以尝试使用 UITableView
的 -indexPathsForVisibleRows
或 -indexPathForRowAtPoint
。
例如,假设您要停止拖动表格时打印最顶层可见单元格的indexPath。你可以这样做:
For example, let's say that you want to print the indexPath of the topmost visible cell, when you stop dragging your table. You could do something like this:
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSIndexPath *firstVisibleIndexPath = [[self.tableView indexPathsForVisibleRows] objectAtIndex:0];
NSLog(@"first visible cell's section: %i, row: %i", firstVisibleIndexPath.section, firstVisibleIndexPath.row);
}
对于Swift 3.0
For Swift 3.0
let topVisibleIndexPath:IndexPath = self.tableView.indexPathsForVisibleRows![0]
这篇关于滚动后检测UITableView中的当前顶部单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!