--UITableView想必大家對(duì)它都不感覺(jué)陌生,估計(jì)你的很多APP的頁(yè)面都有用到它倘待。網(wǎng)絡(luò)上關(guān)于UITableView的文章也不勝其數(shù)了,很多也是建議大家多看看的,比如UITableView優(yōu)化類(lèi)的。
-- 今天我們聊的是關(guān)于UITableView刷新的問(wèn)題。當(dāng)然,你第一感覺(jué)想到的刷新肯定是用 reloadData 這個(gè)方法
刷新UITableView
[self.tableView reloadData];
reloadData這個(gè)方法會(huì)刷新整個(gè)UITableView怠褐,可是有時(shí)候我們只需要刷新其中一個(gè)cell,或者一個(gè)section您宪。這個(gè)時(shí)候再去調(diào)用reloadData 這個(gè)方法奈懒,雖然用戶(hù)看不出來(lái),但是著實(shí)有些浪費(fèi)資源宪巨。這個(gè)時(shí)候磷杏,我們就需要使用局部刷新方法了。
刷新局部cell
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationFade];
這樣就是局部的刷新了第一個(gè)section的第一個(gè)cell捏卓, 雖然代碼看起來(lái)多了一點(diǎn)极祸,但確實(shí)還是畢竟節(jié)省資源的慈格。這也算是對(duì)UITableView的一個(gè)優(yōu)化。
刷新局部section
NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:0];
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
很顯然遥金,這段代碼就是單獨(dú)刷新第一個(gè)section
關(guān)于刷新動(dòng)畫(huà)
刷新動(dòng)畫(huà)還有其他幾個(gè)動(dòng)畫(huà)可以使用
typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
UITableViewRowAnimationFade, //淡入淡出
UITableViewRowAnimationRight, //從右滑入
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
};