原文链接:http://iphonedevelopertips.com/cocoa/understanding-reload-repaint-and-re-layout-for-uitableview.html
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 | |
NOTE A call to setNeedsLayout
will only have an effect if yourUIView
overrides the layoutSubviews
method. This would be the case if you had aUITableViewCell
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 aUIImageView
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 tosetNeedsDisplay
.
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 | |
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 | |