Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 检查特定的UITableView单元格是否在UITableView中可见_Iphone_Objective C_Ios_Xcode_Uitableview - Fatal编程技术网

Iphone 检查特定的UITableView单元格是否在UITableView中可见

Iphone 检查特定的UITableView单元格是否在UITableView中可见,iphone,objective-c,ios,xcode,uitableview,Iphone,Objective C,Ios,Xcode,Uitableview,我有一个UITableView和一些UITableViewCells,它们是我通过界面生成器手动创建的。我已经为每个单元格分配了一个出口,并将它们连接到cellforrowatinexpath方法中的UITableView。在这种方法中,我使用开关(case)方法使特定单元格显示在UITableView中,具体取决于具体情况 现在,我想找到一个特定的单元格,并检查它是否存在于UITableView中。我使用以下方法:UITableView.visibleCells获取表视图中的单元格数组。我的问

我有一个
UITableView
和一些
UITableViewCells
,它们是我通过界面生成器手动创建的。我已经为每个单元格分配了一个
出口
,并将它们连接到
cellforrowatinexpath
方法中的
UITableView
。在这种方法中,我使用
开关(case)
方法使特定单元格显示在
UITableView
中,具体取决于具体情况

现在,我想找到一个特定的单元格,并检查它是否存在于
UITableView
中。我使用以下方法:
UITableView.visibleCells
获取表视图中的单元格数组。我的问题是-如何检查数组中是否存在特定的单元格?我可以使用我分配给它的插座吗(最佳解决方案),或者,我可以使用标识符吗?如何使用


谢谢:)

您可以使用UITableView方法:

[tableView indexPathForCell:aCell];
如果该单元格在tableView中不存在,它将返回nil。否则您将获得单元格的nsindepath

if ([tableView.visibleCells containsObject:myCell])
{
    // Do your thing
}

这假设您有一个单独的实例变量,其中包含您感兴趣的单元格,我认为您可以从问题中找到,但这并不清楚

请注意,您也可以这样使用
indexPathsForVisibleRows

    NSUInteger index = [_people indexOfObject:person];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    if ([self.tableView.indexPathsForVisibleRows containsObject:indexPath]) {
      [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                            withRowAnimation:UITableViewRowAnimationFade];
    }
如果您有indexPath(并且不需要实际的单元格),它可能会更便宜


PS:
\u people
是本例中用作我后端的
NSArray

您可以在Swift 3中执行此操作,以检查
UITableViewCell
是否可见:

let indexPathToVerify = IndexPath(row: 0, section: 0)
let cell = tableView.cellForRow(at: indexPathToVerify)

if tableView.visibleCells.contains(cell) {
    // the cell is visible
}

由于iOS 7,
indexPathForVisibleRows
将包含半透明导航栏下的一行,因此您现在需要执行以下操作:

[self.tableView indexPathsForRowsInRect:self.tableView.safeAreaLayoutGuide.layoutFrame]

谢谢,这是实现Sorig给我的解决方案的另一种方式:)这是对原始问题的正确答案,如果一个单元格是可见的。谢谢你简洁明了的回答。在大多数情况下,这个解决方案比公认的解决方案要好。