實(shí)現(xiàn)效果如下
通過tableView的reloadData方法我們可以方便的對tableVie的cell根據(jù)數(shù)據(jù)源進(jìn)行刷新首懈。但是這種刷新方法在某些時(shí)候也不是那么合適绊率。比如只需要更新幾行的時(shí)候可能顯得多余。同時(shí)在tableView較為復(fù)雜的時(shí)候還會(huì)產(chǎn)生性能的問題究履。在這種時(shí)候我們可以使用tableView的begin Updates方法和end Updates方法來對tableView的幾行數(shù)據(jù)進(jìn)行增刪改和對cell的位置移動(dòng)滤否。系統(tǒng)會(huì)自動(dòng)給我們的操作加上動(dòng)畫。
一.TableView進(jìn)行插入cell操作
1.只進(jìn)行tableView的插入代碼如下
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
[self.arrayData insertObject:@"新添加的行" atIndex:0];
[self.tableView endUpdates];
[CATransaction commit];
2.添加Transaction事務(wù)代碼如下
[CATransaction begin];
[CATransaction setCompletionBlock:^{
NSLog(@"插入cell完成");
}];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
[self.arrayData insertObject:@"新添加的行" atIndex:0];
[self.tableView endUpdates];
[CATransaction commit];
3.設(shè)置動(dòng)畫延時(shí)最仑、持續(xù)時(shí)間藐俺、并且設(shè)置動(dòng)畫類型可以使用如下代碼
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionShowHideTransitionViews animations:^{
[CATransaction begin];
[CATransaction setCompletionBlock:^{
NSLog(@"插入cell完成");
}];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
[self.arrayData insertObject:@"新添加的行" atIndex:0];
[self.tableView endUpdates];
[CATransaction commit];
} completion:^(BOOL finished) {
NSLog(@"動(dòng)畫執(zhí)行完畢");
}];
同時(shí)代碼的執(zhí)行順序如下
2017-11-12 15:54:14.219972+0800 TableViewBeginEndUpdates[2400:84658] 插入cell完成
2017-11-12 15:54:14.220563+0800 TableViewBeginEndUpdates[2400:84658] 動(dòng)畫執(zhí)行完畢
二、tableView進(jìn)行刪除cell操作
[UIView animateKeyframesWithDuration:1 delay:0 options:UIViewKeyframeAnimationOptionLayoutSubviews animations:^{
[CATransaction begin];
[CATransaction setCompletionBlock:^{
}];
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
[self.arrayData removeObjectAtIndex:0];
[self.tableView endUpdates];
[CATransaction commit];
} completion:^(BOOL finished) {
}];
三泥彤、tableView進(jìn)行修改cell操作
[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionAutoreverse animations:^{
[CATransaction begin];
[CATransaction setCompletionBlock:^{
}];
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
[self.arrayData replaceObjectAtIndex:0 withObject:@"修正后的cell"];
[self.tableView endUpdates];
[CATransaction commit];
} completion:^(BOOL finished) {
}];
四欲芹、tableView進(jìn)行移動(dòng)cell操作
[UIView animateKeyframesWithDuration:1 delay:0 options:UIViewKeyframeAnimationOptionAutoreverse animations:^{
} completion:^(BOOL finished) {
[CATransaction begin];
[CATransaction setCompletionBlock:^{
}];
[self.tableView beginUpdates];
[self.tableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] toIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
NSString *str = self.arrayData[0];
[self.arrayData removeObjectAtIndex:0];
[self.arrayData insertObject:str atIndex:2];
[self.tableView endUpdates];
[CATransaction commit];
}];
本示例demo地址為:https://github.com/wangqingxue/TableViewBeginEndUpdates