检测 UITableViewCell 何时离开屏幕 [英] Detect when UITableViewCell goes off the screen
问题描述
我正在使用自定义创建的 UITableViewCell 实现丰富的 UITableView,我以一种方式在屏幕上显示它们,但是一旦它们离开屏幕,我想记下这一点,因为它们第二次出现时我会喜欢它们以不同的方式显示.离开屏幕时考虑自动标记为已读".
I'm implementing a rich UITableView with custom created UITableViewCell, I show these on the screen in one fashion, but once they go off the screen I want to take a note of that, since the second time they come on I would like them to get displayed in a different manner. Think auto "mark as read" when going off the screen.
我一直在寻找某种方法来检测单元格何时离开屏幕(被解除分配或出队或等效),最好在 :UITableViewController 类中快速记下 [indexPath 行],但是在 :UITableViewCell 中也一样好.
I've been looking for some way to detect when a cell goes off the screen (get's dealloc'ed or dequeued or equivalent), preferably in the :UITableViewController class to make a quick note of the [indexPath row], but in the :UITableViewCell is equally as good.
我无法以任何标准方式做到这一点......计算它出现的次数似乎是不可能的,因为我在桌子上进行了多次 reloadData 调用.
I haven't been able to do this in any standard way ... counting the times it appeared seems out of the question as I do multiple reloadData calls on the table.
有人有什么想法吗?这似乎有点棘手:)
Anyone any ideas? This seems a bit tricky :)
推荐答案
这是一个老问题,但以防万一有人在看,在 iOS6 中,引入了一个新的 UITableViewDelegate 函数来做到这一点:
This is an old question, but in case anyone is looking, in iOS6, a new UITableViewDelegate function was introduced that does just this:
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
它在告诉您何时删除单元格方面做得很好,但是,它非常彻底,因此如果您重新加载单元格,即使正在替换的旧单元格也会触发此委托函数.在我的实现中,我只是检查传递的 indexPath
是否仍在数组 tableView.indexPathsForVisibleRows
中.类似的东西:
It does a great job at telling you whenever a cell is removed, however, it is very thorough and thus if you did a reload cell, even the old cell that's being replaced will trigger this delegate function. In my implementation I simply check to see if the indexPath
passed is still within the array tableView.indexPathsForVisibleRows
. Something like:
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView.indexPathsForVisibleRows indexOfObject:indexPath] == NSNotFound)
{
// This indeed is an indexPath no longer visible
// Do something to this non-visible cell...
}
}
这篇关于检测 UITableViewCell 何时离开屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!