2012-10-12 47 views
0

在我的表視圖,我插入一些行的UITableView: - insertRowsAtIndexPaths:withRowAnimation: - 沒有得到動畫的所有單元格

[self.tableView beginUpdates]; 
[self.tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft]; 
[self.tableView endUpdates]; 
[self.tableView scrollToRowAtIndexPath:[arCells lastObject] atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 

蔭沒有得到動畫UITableViewRowAnimationLeft所有單元。假設如果IAM插入5行,我只爲前2個單元格獲取動畫UITableViewRowAnimationLeft,其餘部分插入時沒有動畫。誰能說出爲什麼會發生這種情況?我做錯了什麼嗎?

+0

只是一個預感:你可以嘗試註釋scrollToRow,看看是否行爲相同的方式? – danh

+0

是的,我評論和測試。那時候,當我在最後一個單元格後面插入5行時,我看不到動畫。可見的行正確獲取動畫。 – Dev

+0

對,所以我認爲這是預期的行爲。我認爲問題在於我們將兩個動畫放在一起影響相同的東西。這是一個競賽條件。讓我檢查文檔,看看是否有一個鉤子,告訴你插入動畫已完成,然後我們可以開始滾動 – danh

回答

0

因此,我們的目標是以插入和定位內容的方式,使所有插入的行都可見。只要插入的行比表本身更短,這是可行的。

似乎滾動動畫和插入互相干擾。要解決,我們首先要做的滾動,因爲文檔提供了當動畫完成,即透明鉤,委託方法- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView

該解決方案會去是這樣的:

// about to insert cells at arCells index paths 
// first scroll so that the top is visible 
NSIndexPath *firstNewIndexPath = [arCells objectAtIndex:0]; 
NSInteger previousRow = MAX(firstNewIndexPath.row-1, 0); 
NSIndexPath *previousIndexPath = [NSIndexPath indexPathForRow:previousRow inSection:firstNewIndexPath.section]; 

// if the new rows are at the bottom, adjust the content inset so the scrolling can happen 

if (firstNewIndexPath.row > [self.tableView numberOfRowsInSection:0) { 
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, self.tableView.frame.size.height - 80, 0); // 80 is just to illustrate, get a better row height from the table 
} 

[self.tableView scrollToRowAtIndexPath:previousIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 

// there may be a better way to setup that scroll, not sure, but that should work. 

現在我們有一個知道動畫完成的鉤子。我們可以放心地做插入...

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { 

    // hopefully you have those arCells in an instance variable already, otherwise 
    // i think you'll need to create one to save state in between the two animations 
    [self.tableView beginUpdates]; 
    [self.tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft]; 
    [self.tableView endUpdates]; 

    // restore the content inset 
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0); 
} 

一對夫婦的其他SO articles like this one處理得到一個鉤來告訴我們的行動畫完成。這可能會更好,因爲那麼我們就有更好的想法在哪裏滾動(如你的問題所示,到新插入行的底部)。但是這些都不能讓我們知道動畫已經完成。

+0

想想更多,我認爲它需要工作,當新的細胞在最底層。滾動不會做我們想要的,因爲新的細胞還沒有。一個想法是使用contentInset。將編輯來說明。 – danh

相關問題