標籤:
重新整理UITableView
方法:[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
};
iOS 重新整理tableview方法