Understanding Reload, Repaint, and Re-Layout for UITableView

原文链接: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 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
[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;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值