淺談TableView的begin Updates和end Updates

image.png

tableViewBeginAnimation.gif

實(shí)現(xiàn)效果如下

通過tableView的reloadData方法我們可以方便的對tableVie的cell根據(jù)數(shù)據(jù)源進(jìn)行刷新。但是這種刷新方法在某些時候也不是那么合適。比如只需要更新幾行的時候可能顯得多余。同時在tableView較為復(fù)雜的時候還會產(chǎn)生性能的問題。在這種時候我們可以使用tableView的begin Updates方法和end Updates方法來對tableView的幾行數(shù)據(jù)進(jìn)行增刪改和對cell的位置移動。系統(tǒ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è)置動畫延時、持續(xù)時間、并且設(shè)置動畫類型可以使用如下代碼

[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(@"動畫執(zhí)行完畢");
    }];

同時代碼的執(zhí)行順序如下

2017-11-12 15:54:14.219972+0800 TableViewBeginEndUpdates[2400:84658] 插入cell完成
2017-11-12 15:54:14.220563+0800 TableViewBeginEndUpdates[2400:84658] 動畫執(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)行移動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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,283評論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,232評論 4 61
  • 萬圣節(jié)是每年西方國家的傳統(tǒng)節(jié)日。 這一夜是一年中人們認(rèn)為會“鬧鬼”的一夜,所以也叫“鬼節(jié)”。 萬圣節(jié)是諸圣節(jié)(Al...
  • 車在路上跑,所以要投保,車跑人也跑,為何人不保,車壞可以修,人壞把命丟,留債不留愛,你說壞不壞,老婆沒人愛,兒女無...
    dcbbb8c129a4閱讀 279評論 0 0
  • 不過,我開始相信一切都會變好的。不是因?yàn)槲覙酚^還是怎么的,就是如果有心理暗示的作用,相信事情會變好,比相信事情不會...
    茶也不甜閱讀 166評論 0 0