UITableViewCell won’t update, arghhhh!

Last night I had some “fun” with a little iPhone application that I’m writing. It has a UITableView that I fill with custom UITableViewCells. My custom cells are just a sub-class of UITableViewCell, and are built in code rather than Interface Builder. In this case, I was making an asynchronous call to a server and then updating the display of a cell once the server returned. Unfortunately, my cell wasn’t updating on the screen.

I did some Google searching and found all sorts of people with my problem, but no one really had a solution. Many of the suggestions involved calling [tableView reloadData] or calling [customCell setNeedsDisplay] or some other nonsense. While [tableView reloadData] worked, I wanted my display update to be all iPhone-y and animated, which reloadData didn’t facilitate very well.

In my callback from the completion of the server call (handled by the UITableViewController itself), I was getting the cell to update via

[self tableView:self.tableView cellForRowAtIndexPath:selectedPath];

(selectedPath is a member variable holding the index path of the cell the user selected)

What I got looked like exactly what I needed – all the properties and data on the returned cell was correct, but no changes would affect the screen. You can probably see the problem now, but if you can’t here’s the solution:

[self.tableView cellForRowAtIndexPath:selectedPath];

The difference is minor (and in fact I had double-checked another place in the same file that had the correct version of the code and didn’t notice the difference), but that was the problem. Calling the method tableView: cellForRowAtIndexpath: on the UITableViewController was dequeuing a cell and rebuilding it – it wasn’t returning the *actual* on screen cell. All the data was correct, but the cell I was editing wasn’t on the screen. Instead, calling cellForRowAtIndexPath: ¬†on the UITableView itself will return the actual cell that’s on the screen, which you can modify as much as you’d like. If the cell you ask for isn’t on the screen, I believe you’ll get nil returned.

Anyway, I hope this can help someone avoid spending an hour wondering why things won’t change on the screen. Enjoy!

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>