滚动后检测 UITableView 中的当前顶部单元格 [英] Detect the current top cell in a UITableView after scrolling

查看:30
本文介绍了滚动后检测 UITableView 中的当前顶部单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题,但我似乎没有正确的术语来搜索 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.

You could try using UITableView's -indexPathsForVisibleRows or -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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆