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

查看:31
本文介绍了确定 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 的实例方法,它会返回一个 NSArrayNSIndexPath 表中每一行当前可见的对象.您可以使用您需要的任何频率检查此方法并检查正确的行.例如,如果 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天全站免登陆