您的位置:首页 > 产品设计 > UI/UE

Understanding Reload, Repaint, and Re-Layout for UITableView

2011-08-26 10:14 417 查看
原文链接: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

[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
23
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;

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: