Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know whether a UITableViewCell currently exists?

Tags:

ios

Given the indexPath of a UITableViewCell, I want to know whether the cell currently exists.

I know how to check if it's on the screen (use tableView.indexPathsForVisibleRows). However, I also want to know if it's off the screen but already created (assuming the user has scrolled but it hasn't quite entered the screen are yet).

How do I do this?

like image 252
ill_always_be_a_warriors Avatar asked Dec 09 '22 13:12

ill_always_be_a_warriors


1 Answers

You can do

[self.tableView cellForRowAtIndexPath:indexPath];

(not to be confused with the data source method [self tableView:cellForRowAtIndexPath:]) and if a cell for that index path exists, it will be returned. Otherwise, you'll get nil.

You definitely don't want your background update process to hold a direct reference the cell because, as you said, it may have scrolled off screen and been recycled by the time the fetch completes. Instead, hold a reference to the index path or some piece of data that can be used to look up the index path and then use that index path with the above method to retrieve the cell.

like image 96
Timothy Moose Avatar answered Dec 25 '22 13:12

Timothy Moose