开发者

Looking for opposite of tableView:willDisplayCell:forRowAtIndexPath: to detect when cell is being "un"-displayed

Is there a way to detect when a table view cell is going off of the screen? The opposite of tableView:willDisplayCell:forRowAtIndexPath:, like tableView:willHideCell:forRowAtIndexPath: would be great, but obviously it doesn't exist.

The reason I'd like to detect this is because 开发者_StackOverflowwhenever a new cell is shown, I'm downloading images to place in that cell, but if the user is simply scrolling by the cell and not viewing it, I would like to cancel the download of those images.


In most cases, you won't need this, because the table view will usually always display the same number of cells, so a cell that goes offscreen will immediately be reused and in this case, the easiest approach would be to cancel the download after you dequeue the cell.

If you have a lot of different cell identifiers, this may not be the case though. One method I could think of would be to override willMoveToSuperview: in your UITableViewCell subclass and check the superview parameter for nil (which means that it has been removed from the view hierarchy).


Just simply do that after you deque a cell from the tableview. As soon a cell pops of the screen it will be reused.


Not exactly what you are asking for, but if you look at Apple's LazyTableImages sample, they delay starting the download of an image until scrolling ends:

if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
{
    [self startIconDownload:appRecord forIndexPath:indexPath];
}


I believe it's 'didEndDisplaying'

https://developer.apple.com/reference/uikit/uicollectionviewdelegate/1618036-collectionview

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜