2012-01-09 60 views
7

我正在使用beginUpdates/endUpdates塊更改tableView。在整個過程中,我需要更新一個陰影,以便它反映tableView的當前構圖。UITableView beginUpdates/endUpdates回調

我嘗試爲tableView的contentSize設置KVO,但只有在動畫完成後纔會調用endUpdates。我想要的是每次更改contentSize時都會調用它(即使它只有一個像素)。有什麼辦法可以做到這一點?

回答

0

對不起,我認爲你不能這樣做。在beginUpdates之後,如果您更改了表格,則會在endUpdates之後將更改動畫爲單個動畫。在這些動畫中沒有動畫回調。我沒有試過這個,所以不知道它是否適用於此,但你可以嘗試嵌套beginUpdatesendUpdates,並在每個endUpdates之後更新你的影子。

+2

[tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:scrollPosition]; 

這啓發了我創建這個代碼...這是工作無縫beginUpdates和endUpdates的調用可以嵌套。如果您不在該塊內進行插入,刪除和選擇調用,則表格屬性(如行計數)可能會失效。 – 2012-09-30 15:12:56

+0

你有沒有一個應該如何使用嵌套調用的例子? – Rivera 2014-09-03 10:13:48

24

這是怎麼回事?

[CATransaction begin]; 

[CATransaction setCompletionBlock:^{ 
    // animation has finished 
}]; 

[tableView beginUpdates]; 
// do some work 
[tableView endUpdates]; 

[CATransaction commit]; 
+0

這是爲我做的!謝謝! – horseshoe7 2013-04-18 15:39:42

+1

哇!這幾乎可以工作,但在結束更新和完成塊運行的tableview之間存在延遲。現在我會用它。謝謝。 – SpaceDog 2016-05-29 00:28:18

1

魯道夫的方法對我來說並沒有像預期的那樣順利。在我的情況下,我在UITableView上使用這個選擇了一行,而Rudolf的方法導致表格做了兩個帶有一點凍結的動畫:beginUpdates/endUpdates裏面的動畫,一點凍結和完成塊上的動畫。僅供參考

[UIView animateWithDuration:0.0 animations:^{ 
    [tableView beginUpdates]; 
    // do something to the table 
    [tableView endUpdates]; 
} completion:^(BOOL finished) { 
    // Code to run when table updates are complete. 
}];