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:
NOTE A call to
In most cases, I find that it is sufficient to call
just changing the image will not always trigger a repaint of the view, for that to occur you need to make a call to
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.
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:
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 |
setNeedsLayoutwill only have an effect if your
UIViewoverrides the
layoutSubviewsmethod. This would be the case if you had a
UITableViewCellthat 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
UIImageViewis 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.
UITableViewhas a method designed specifically for this scenario and it usage looks like this:
1 | [myTableView reloadData]; |
at the following methods in UITableView:
1 23 | - (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; |
相关文章推荐
- Link - iOS Layout about "edgesForExtendedLayout" and "automaticallyAdjustsScrollViewInsets"
- Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
- 使用UITableView+FDTemplateLayoutCell计算cell的高度
- Suspicious size this will make the view invisible, probably intended for layout_width
- 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a ...
- IOS UITableView-FDTemplateLayoutCell框架高度返回问题
- [IOS]edgesForExtendedLayout、automaticallyAdjustsScrollViewInsets、、
- 【iOS】Masonry和FDTemplateLayoutCell搭配使用「UITableview自适应内容高度」
- layoutSubviews、drawRect、edgesForExtendedLayout、automaticallyAdjustsScrollViewInsets
- ViewOverlay: When, How and for What Purpose?
- edgesForExtendedLayout对子vc的view的影响
- ZFS: Performance Tuning for Scrubs and Resilvers
- TableView性能优化——动态cell适应工具(UITableView+FDTemplateLayoutCell)
- Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]
- C++ 多继承和虚继承的内存布局(Memory Layout for Multiple and Virtual Inheritance)
- viewForBaselineLayout
- iOS~Assertion failure in -[UITableView layoutSublayersOfLayer:]
- swift 学习笔记 UITableView (一)Table View Styles and Accessory Views
- HTTP Status 404 - There is no Action mapped for namespace /jsp and action name login. type Status re
- Memory Layout for Multiple and Virtual Inheritance