簡體   English   中英

UITableview:-insertRowsAtIndexPaths:withRowAnimation:-無法獲得所有單元格的動畫

[英]UITableview :- insertRowsAtIndexPaths: withRowAnimation: - Not getting animation for all the cells

在表格視圖中,我要插入一些行

[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 ,其余部分將插入而沒有動畫。 誰能說出這是為什么呢? 我做錯了嗎?

因此,目標是進行插入並將內容定位,以使所有插入的行均可見。 只要插入的行比表本身短,這是可行的。

滾動動畫和插入似乎相互干擾。 要修復,讓我們先進行滾動,因為文檔為該動畫何時完成提供了清晰的鈎子,即委托方法- (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文章涉及一個鈎子,告訴我們行動畫已完成。 這樣做可能會更好,因為這樣我們就可以更好地滾動到何處(如您的問題所提示的那樣,滾動到新插入的行的底部)。 但是這些似乎都無法讓我們知道動畫已完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM