ios 检查特定的 UITableViewCell 在 UITableView 中是否可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8052999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a specific UITableViewCell is visible in a UITableView
提问by ozking
I have a UITableView
and some UITableViewCells
which i have created manually via the Interface Builder. I've assigned each cell an outlet
, and im connecting them to the UITableView
in the CellForRowAtIndexPath
method. In this method, I use the switch(case)
method to make specific cells appear in the UITableView
, depends on the case.
我有一个UITableView
和一些UITableViewCells
我通过Interface Builder手动创建的。我分配每一个单元的outlet
,和IM将它们连接到UITableView
的CellForRowAtIndexPath
方法。在这种方法中,我使用该switch(case)
方法使特定的单元格出现在 中UITableView
,具体视情况而定。
Now, I want to find a specific cell and check if he is exists within the UITableView
. I use the method: UITableView.visibleCells
to get an array of the cells in the table view. 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?
现在,我想找到一个特定的单元格并检查他是否存在于UITableView
. 我使用方法:UITableView.visibleCells
获取表格视图中的单元格数组。我的问题是 - 如何检查数组中是否存在特定单元格?我可以以某种方式使用我分配给它的插座吗?-(最佳解决方案),或者,我可以使用标识符吗?如何使用?
Thanks :)
谢谢 :)
回答by StuFF mc
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
作为我在这种情况下后端。
回答by jrturton
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.
这假设您有一个单独的实例变量,其中包含您感兴趣的单元格,我认为您从问题中可以做到,但尚不清楚。
回答by Sorig
You can use the UITableView method:
您可以使用 UITableView 方法:
[tableView indexPathForCell:aCell];
If the cell doesn't exist in the tableView it will return nil. Otherwise you will get the cell's NSIndexPath.
如果单元格在 tableView 中不存在,它将返回 nil。否则,您将获得单元格的 NSIndexPath。
回答by pableiros
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
}
回答by malhal
Since iOS 7, indexPathForVisibleRows
will contain a row that is under the translucent navigation bar hence you now need to do this:
从 iOS 7 开始,indexPathForVisibleRows
将包含一个位于半透明导航栏下方的行,因此您现在需要执行以下操作:
[self.tableView indexPathsForRowsInRect:self.tableView.safeAreaLayoutGuide.layoutFrame]