确定tableview单元格是否可见 [英] Determine if a tableview cell is visible

查看:99
本文介绍了确定tableview单元格是否可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法知道tableview单元格当前是否可见?
我有一个tableview,其第一个单元格(0)是一个uisearchbar。
如果搜索未激活,则通过偏移隐藏单元格0。
当表只有几行时,第0行可见。
如何确定第0行是可见还是第一行?

Is there any way to know if a tableview cell is currently visible? I have a tableview whose first cell(0) is a uisearchbar. If a search is not active, then hide cell 0 via an offset. When the table only has a few rows, the row 0 is visible. How to determine if row 0 is visible or is the top row?

推荐答案

UITableView 有一个名为 indexPathsForVisibleRows 的实例方法,它将返回 NSArray NSIndexPath 表中每行当前可见的对象。您可以使用您需要的任何频率检查此方法并检查正确的行。例如,如果 tableView 是对表的引用,则以下方法将告诉您行0是否在屏幕上:

UITableView has an instance method called indexPathsForVisibleRows that will return an NSArray of NSIndexPath objects for each row in the table which are currently visible. You could check this method with whatever frequency you need to and check for the proper row. For instance, if tableView is a reference to your table, the following method would tell you whether or not row 0 is on screen:

-(BOOL)isRowZeroVisible {
  NSArray *indexes = [tableView indexPathsForVisibleRows];
  for (NSIndexPath *index in indexes) {
    if (index.row == 0) {
      return YES;
    }
  }

  return NO;
}

因为 UITableView 方法返回 NSIndexPath ,您可以轻松扩展它以查找节或行/节组合。

Because the UITableView method returns the NSIndexPath, you can just as easily extend this to look for sections, or row/section combinations.

这比 visibleCells 方法更有用,它返回一个表格单元格对象数组。表格单元格对象会被回收,因此在大型表格中,它们最终不会与数据源产生简单的关联。

This is more useful to you than the visibleCells method, which returns an array of table cell objects. Table cell objects get recycled, so in large tables they will ultimately have no simple correlation back to your data source.

这篇关于确定tableview单元格是否可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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