简体   繁体   English

检查UITableView中是否显示特定的UITableViewCell

[英]Check if a specific UITableViewCell is visible in a UITableView

I have a UITableView and some UITableViewCells which i have created manually via the Interface Builder . 我有一个UITableView和一些我通过Interface Builder手动创建的UITableViewCells I've assigned each cell an outlet , and im connecting them to the UITableView in the CellForRowAtIndexPath method. 我已经为每个单元分配了一个outlet ,并将它们连接到CellForRowAtIndexPath方法中的UITableView In this method, I use the switch(case) method to make specific cells appear in the UITableView , depends on the case. 在这个方法中,我使用switch(case)方法使特定单元格出现在UITableView ,具体取决于具体情况。

Now, I want to find a specific cell and check if he is exists within the UITableView . 现在,我想找到一个特定的单元格并检查它是否存在于UITableView I use the method: UITableView.visibleCells to get an array of the cells in the table view. 我使用方法: UITableView.visibleCells来获取表视图中的单元格数组。 My question is - how can i check if a specific cells exists in the array? 我的问题是 - 如何检查数组中是否存在特定的单元格? can I use the outlet that i've assigned to it somehow? 我能以某种方式使用我分配给它的插座吗? - (The best solution),OR, can I use an identifier and how? - (最好的解决方案),或者,我可以使用标识符吗?

Thanks :) 谢谢 :)

Note that you can as well use indexPathsForVisibleRows this way: 请注意,您也可以这样使用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];
    }

If you have the indexPath (and don't need the actual Cell) it might be cheaper . 如果你有indexPath(并且不需要实际的Cell),它可能会更便宜

PS: _people is the NSArray used as my backend in this case. PS: _people是在这种情况下用作我的后端的NSArray

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

This assumes that you have a separate instance variable containing the cell you are interested in, I think you do from the question but it isn't clear. 这假设您有一个单独的实例变量,其中包含您感兴趣的单元格,我认为您可以从问题中做到,但目前尚不清楚。

You can use the UITableView method: 您可以使用UITableView方法:

[tableView indexPathForCell:aCell];

If the cell doesn't exist in the tableView it will return nil. 如果tableView中不存在该单元格,则返回nil。 Otherwise you will get the cell's NSIndexPath. 否则,您将获得单元格的NSIndexPath。

You can do this in Swift 3 to check if the UITableViewCell is visible: 您可以在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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM