iOS中tableview的幾種刷新
第一種刷新:tableview的刷新 [self.tableView reloadData];
reloadData是刷新整個UITableView,有時候,我們可能需要局部刷新。比如:只刷新一個cell、只刷新一個section等等。這個時候在調用reloadData方法,雖然用戶看不出來,但是有些浪費資源。
第二種刷新:刷新局部的cell
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationFade];
這樣就可以很方便的刷新第一個section的第一個cell。雖然看起來代碼多了,但是確實比較節省資源。盡量少的刷新,也是UITableView的一種優化。
第三種刷新:局部刷新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
};
1.NSIndexSet是什么?
NSIndexSet 是個無符號整數集合。集合中的元素不可變的、不可重復。常被用來當作索引使用。就從它字面上理解,就叫做:索引集合。