Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get UITableViewCell from UITableView with cellForRowAtIndexPath

I'm trying to get a subclassed UITableViewCell (class is called 'MasterCell') from my UITableViewController with an changing IndexPath. Let's say, I'll get the cell at the first row:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
MasterCell *cell = (MasterCell *)[self.tableView cellForRowAtIndexPath:indexPath];

This happens in my viewWillAppear method. Unfortunately, cell is nil while debugging.

I have different cells / rows, where I have to change some values in the viewWillAppear method. Do you have any hints for me?

like image 269
Fonsi Avatar asked Dec 21 '22 16:12

Fonsi


1 Answers

[self.tableView cellForRowAtIndexPath:indexPath] returns nil for cells which are currently not visible.

You should not use the cells to store your model data, because cells are reused when you scroll up or down.

like image 127
Martin R Avatar answered Mar 30 '23 01:03

Martin R