Understanding Reload, Repaint, and Re-Layout for UITableView

Sun, Oct 19

Leaning the Objective-C language isn’t that bad; the true hurdle in learning to develop for iPhone is in understanding the frameworks. UITableView and UITableViewController is a perfect example of how a developer, new to iPhone, can get lost in a quagmire of documentation. Most recently I found my self looking for a way to tell a UITableView that its model had changed and that it needed to refresh itself. This began a journey of discovery that has helped me better understand the UITableView APIs.

Repaint and Re-layout

The first step is to understand the various concepts related to updating UIViews. Repaint and Re-Layout are concepts that are available at the UIView level and manifest themselves as follows:

1
2
[myView setNeedsDisplay]; // repaint
[myView setNeedsLayout]; // re-layout

NOTE A call to setNeedsLayout will only have an effect if your UIView overrides the layoutSubviews method. This would be the case if you had a UITableViewCell that had multiple subviews that you manually layout.

In most cases, I find that it is sufficient to call setNeedsDisplay. A good example of this is when a UIImageView is rendered with one image; and while it is still on screen, you change the image to something else. In this case, just changing the image will not always trigger a repaint of the view, for that to occur you need to make a call to setNeedsDisplay.

Reloading Data

Reloading data is different from a repaint as it usually implies that the underlying data model has changed. This was the case I found myself in most recently when I was trying to refresh the contents of a table that was displaying a list of feeds from Jaiku. In this scenario trying to tell every table cell to reload or repaint would have been pointless as the model they were operating against was no longer valid.

UITableView has a method designed specifically for this scenario and it usage looks like this:

1
[myTableView reloadData];

NOTE: This approach will usually result in the recreation of all new table cell instances. If this is something you feel strongly about, you may want to look into the batch updating features of a table. For more information, take a look at the following methods in UITableView:

1
2
3
4
5
6
7
8
- (void)beginUpdates;
- (void)endUpdates;
 
- (void)insertSections:(NSIndexSet *)sections withRowAnimation: (UITableViewRowAnimation)animation;
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation: (UITableViewRowAnimation)animation;
 
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation;
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation;

14 comments

reloadData was exactly what I was looking for. when my table view showed the second time (after the data model had changed) the visible cells were still showing the old data while new cells (after scrolling) would show the new data. Thanks!

by Lutz on Feb 13, 2009. Reply #

Should [myTableView reloadData] work if you are using Reusable UITableViewCells with the reuseIdentifer? It doesn’t appear to “flush” the cells that have been queued for reuse.

If I don’t use that, then it works fine.

by Jay Wright on May 14, 2009. Reply #

reloadData was also exactly what I needed. I kept trying to get loadData to also repaint. Much easier this way. Thanks

by Chris on May 16, 2009. Reply #

So, you only call [view reloadData] if you’ve changed the dataSource?

If you haven’t changed the dataSource, but the dataSource has new data, will [view setNeedsDisplay] be sufficient?

by Adam Olsen on Aug 31, 2009. Reply #

Just wanted to say thanks for this.

I spent a little too long for comfort thinking about how to force a table to clear itself from the cache and reload itself (effectively recalling all those UITableViewDelegate methods I had implemented) when I wanted it to display a changed data set.

Great.

Best wishes.

by iKarl on Feb 19, 2010. Reply #

What would cause this?

I have a grouped tableView that I’ve added in IB. 2 groups. 3 rows in each group.

It displays fine when my app is first run. (So I know I’m doing SOMETHING right. Wouldn’t that mean that I (at least) have my Delegate, DataSource, IBOutlets, and connections set up correctly?)

But when I click on a button that calls [myTable reloadData] it just crashes.

Initially, I thought it was due to my changes in the number of groups, number of rows, or changing the data in my dataSource (just a small NSMutableArray).

But the same thing happens when I change NO data: Works fine the first time my tableView is displayed. But crashes when I call [myTable reloadData]. (Whether the data has changed or not.)

by Carol on Feb 21, 2010. Reply #

Thanks. reloadData is exactly what I’m looking for.

by Dat on Mar 5, 2010. Reply #

I notice I can separately reload a “section”, a “row”, or the entire table.

What if I just need to update the headers or footers?

by Carol on Mar 18, 2011. Reply #

I find if I call reload data after I have entered the detail of a cell that I don’t return back to the table view, but stay in the detail Any ideas on haw to make it show thte list view again?

by Bobby on Apr 9, 2011. Reply #

ReloadData is great but it doesn’t have animation delete or insertSections.

by Raksmey on Jun 15, 2011. Reply #

ReloadData was exactly what I was looking for. Thanks!!!
When the user clicked on one cell, I changed the accesory to show a checkmarck, but it didn’t appeared unless I scrolled the complete list of elements, on this moment, it appeared the checkmark.
I’ve been 2 hours looking for the problem and reading the classes documentation. Thanks for helping us.

by Yolanda on Oct 11, 2011. Reply #

Thanks for the tip. Found out if within the UITableViewController, you can use [self.tableView reloadData];

by Tomas on Dec 4, 2011. Reply #

Thanks for the reloadData tip!

by Mihai on Mar 7, 2012. Reply #

Many thanks for the tip.

My data didn’t change, i needed only to refresh my UITableView : [myTableView reloadData] did it perfectly !

by Michel Futtersack on Jun 22, 2012. Reply #

Leave a Comment