thomashw
thomashw

Reputation: 956

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 whenever 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.

Upvotes: 1

Views: 884

Answers (4)

omz
omz

Reputation: 53551

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).

Upvotes: 1

yinkou
yinkou

Reputation: 5766

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

Upvotes: 1

Caffeine
Caffeine

Reputation: 862

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];
}

Upvotes: 0

Related Questions