在官方文檔中是這樣介紹beginUpdates的html
Call this method if you want subsequent insertions, deletion, and selection operations (for example, cellForRowAtIndexPath:
andindexPathsForVisibleRows
) to be animated simultaneously. This group of methods must conclude with an invocation ofendUpdates
. These method pairs can be nested. ios
If you do not make the insertion, deletion, and selection calls inside this block, table attributes such as row count might become invalid. ---------這句話沒懂app
You should not call reloadData
within the group; if you call this method within the group, you will need to perform any animations yourself.ide
通常當tableview須要同時執行多個動畫時,纔會用到beginUpdates函數,它的本質就是創建了CATransaction這個事務。咱們能夠經過如下的代碼驗證這個結論
函數
[CATransaction begin]; [CATransaction setCompletionBlock:^{ // animation has finished }]; [tableView beginUpdates]; // do some work [tableView endUpdates]; [CATransaction commit];
這段代碼來自stackoverflow,它的做用就是在tableview的動畫結束後,執行須要的操做。這段代碼好用的緣由就是beginUpdates本質上就是添加了一個動畫事務,即CATransaction,固然這個事務可能包含許多操做,好比會從新調整每一個cell的高度(可是默認不會從新加載cell)。若是你僅僅更改了UITableView的cell的樣式,那麼應該試試可否經過調用beginUpdates 和 reloadRowsAtIndexPaths 來實現效果,而不是調用tableview的reloadData方法去從新加載所有的cell!動畫
一個例子,帶有動畫效果的,從新加載部分cell的代碼。this
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tmp] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];
下面是一個例子,想實現一個點擊cell,cell的高度就變高的效果,就能夠在選中方法中設置標誌位,來影響代理方法返回的height值,而且在以後調用spa
[tableView beginUpdates];
[tableView endUpdates];
沒錯,他們之間沒有代碼,算然沒代碼,這2句話會根據代理方法從新調整cell的高度。下面是調用的效果截圖:代理