爪機碼字,不規(guī)范,見諒,手機上看到了,轉(zhuǎn)來保存,寫的很詳細。原文見豆瓣
http://www.douban.com/group/topic/80718778/
在很多APP界面都用到的UITableView,對iOS開發(fā)者來說一定不陌生吧。網(wǎng)上關(guān)于UITableView的文章早已多不勝數(shù),尤其是關(guān)于UITableView優(yōu)化方面的,對開發(fā)者來說非常值得一看。
現(xiàn)在就來看看,刷新UITableView該怎么做吧,一般情況下,我們會通過直接調(diào)用reloadData的方法,去刷新UITableView的。
刷新UITableView
[self.tableView reloadData];
reloadData是刷新整個UITableView,有時候,我們可能需要局部刷新。比如:只刷新一個cell、只刷新一個section等等。這個時候在調(diào)用reloadData方法,雖然用戶看不出來,但是有些浪費資源。
刷新局部cell
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableViewreloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil]
withRowAnimation:UITableViewRowAnimationFade];
這是刷新第一個section的第一個cell很方便的一種方法,雖然看上去,代碼變多了,但是很節(jié)省資源,盡量減少刷新頻率,這也是在iOS開發(fā)中對UITableView的一種優(yōu)化。
局部刷新section
NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:0];
[self.tableView reloadSections:indexSet
withRowAnimation:UITableViewRowAnimationFade];
上面這段代碼是刷新第0個section。
刷新動畫
刷新UITableView還有幾個動畫:
typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
UITableViewRowAnimationFade, //淡入淡出
UITableViewRowAnimationRight, //從右滑入 // slide in from right (or out to right)
UITableViewRowAnimationLeft, //從左滑入
UITableViewRowAnimationTop, //從上滑入
UITableViewRowAnimationBottom, //從下滑入
UITableViewRowAnimationNone, // available in iOS 3.0
UITableViewRowAnimationMiddle, // available in iOS 3.2. attempts to keep cell centered in the space it will/did occupy
UITableViewRowAnimationAutomatic = 100 // available in iOS 5.0. chooses an appropriate animation style for you
};
以上便是在iOS開發(fā)中,刷新UITableView的幾個小技巧。