如何判断 UITableView 的单元格是否在屏幕上可见
How to tell if a UITableView's cell is visible on the screen
我正在为 iOS 8 编写一个 Objective-C 应用程序。我想知道是否有人知道如何判断 UITableView
中的 UITableViewCell
是否开启-屏幕。
例如,如果 UITableView
的 contentOffset
的 y 值为 0(意味着 UITableView
滚动到顶部)并且我以编程方式 select 第 2 行,我根本不想滚动,因为第 2 行已经可见(假设 UITableView
足够大以显示至少 3 行)。但是,如果我以编程方式 select 第 10 行(在屏幕外),我想以编程方式滚动到该行。
这是我用来滚动到 selected 行的代码:
[self.tableView scrollToRowAtIndexPath:[_tableView indexPathForSelectedRow]
atScrollPosition:UITableViewScrollPositionMiddle
animated:NO];
现在,我只需将该代码嵌套在一个 if 语句中,该语句检查当前 selected 行的单元格是否在屏幕上完全可见。
有什么建议吗?
由于您希望选定的行以最少的移动量可见,因此请使用专门为此制作的方法:
[self.tableView scrollToNearestSelectedRowAtScrollPosition: UITableViewScrollPositionNone animated:YES];
来自 UITableViewScrollPositionNone
的文档:
The table view scrolls the row of interest to be fully visible with a minimum of movement. If the row is already fully visible, no scrolling occurs. For example, if the row is above the visible area, the behavior is identical to that specified by UITableViewScrollPositionTop. This is the default.
试试这个,
NSIndexPath *path = [tableView indexPathForCell:tableView.visibleCells.firstObject];
[self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:YES];
无法询问表格视图是否在特定时间显示视图。
你可以做的是成为这个表视图的委托并实现这个方法:
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView
didEndDisplayingCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
您可以实现此方法来跟踪当时显示的 uitableview。
我正在为 iOS 8 编写一个 Objective-C 应用程序。我想知道是否有人知道如何判断 UITableView
中的 UITableViewCell
是否开启-屏幕。
例如,如果 UITableView
的 contentOffset
的 y 值为 0(意味着 UITableView
滚动到顶部)并且我以编程方式 select 第 2 行,我根本不想滚动,因为第 2 行已经可见(假设 UITableView
足够大以显示至少 3 行)。但是,如果我以编程方式 select 第 10 行(在屏幕外),我想以编程方式滚动到该行。
这是我用来滚动到 selected 行的代码:
[self.tableView scrollToRowAtIndexPath:[_tableView indexPathForSelectedRow]
atScrollPosition:UITableViewScrollPositionMiddle
animated:NO];
现在,我只需将该代码嵌套在一个 if 语句中,该语句检查当前 selected 行的单元格是否在屏幕上完全可见。
有什么建议吗?
由于您希望选定的行以最少的移动量可见,因此请使用专门为此制作的方法:
[self.tableView scrollToNearestSelectedRowAtScrollPosition: UITableViewScrollPositionNone animated:YES];
来自 UITableViewScrollPositionNone
的文档:
The table view scrolls the row of interest to be fully visible with a minimum of movement. If the row is already fully visible, no scrolling occurs. For example, if the row is above the visible area, the behavior is identical to that specified by UITableViewScrollPositionTop. This is the default.
试试这个,
NSIndexPath *path = [tableView indexPathForCell:tableView.visibleCells.firstObject];
[self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:YES];
无法询问表格视图是否在特定时间显示视图。 你可以做的是成为这个表视图的委托并实现这个方法:
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView
didEndDisplayingCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
您可以实现此方法来跟踪当时显示的 uitableview。