淺談TableView的begin Updates和end Updates

image.png

tableViewBeginAnimation.gif

實(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末皮官,一起剝皮案震驚了整個(gè)濱河市徽缚,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌自晰,老刑警劉巖剑逃,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件浙宜,死亡現(xiàn)場離奇詭異,居然都是意外死亡蛹磺,警方通過查閱死者的電腦和手機(jī)粟瞬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來萤捆,“玉大人裙品,你說我怎么就攤上這事俗批。” “怎么了市怎?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵岁忘,是天一觀的道長。 經(jīng)常有香客問我区匠,道長臭觉,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任辱志,我火速辦了婚禮蝠筑,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘揩懒。我一直安慰自己什乙,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布已球。 她就那樣靜靜地躺著臣镣,像睡著了一般。 火紅的嫁衣襯著肌膚如雪智亮。 梳的紋絲不亂的頭發(fā)上忆某,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天,我揣著相機(jī)與錄音阔蛉,去河邊找鬼弃舒。 笑死,一個(gè)胖子當(dāng)著我的面吹牛状原,可吹牛的內(nèi)容都是我干的聋呢。 我是一名探鬼主播,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼颠区,長吁一口氣:“原來是場噩夢啊……” “哼削锰!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起毕莱,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤器贩,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后朋截,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蛹稍,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年质和,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了稳摄。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片稚字。...
    茶點(diǎn)故事閱讀 39,953評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡饲宿,死狀恐怖厦酬,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情瘫想,我是刑警寧澤仗阅,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站国夜,受9級特大地震影響减噪,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜车吹,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一筹裕、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧窄驹,春花似錦朝卒、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至丈咐,卻和暖如春瑞眼,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背棵逊。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工伤疙, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人辆影。 一個(gè)月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓掩浙,卻偏偏與公主長得像,于是被迫代替她去往敵國和親秸歧。 傳聞我的和親對象是個(gè)殘疾皇子厨姚,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,901評論 2 355

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

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