2013-07-13 40 views
9

我很好奇,當插入/刪除動畫調用後更新UITableView的內容大小。我認爲它會像大多數[UIView動畫...]塊一樣,即使動畫未完成,幀大小/內容大小也會立即更新,但似乎並非如此。有任何想法嗎?什麼時候UITableView內容大小更新與行插入/刪除動畫

+1

你解決了這個問題嗎?我遇到了同樣的問題。插入/刪除行然後期待tableView.contentSize被更新,但似乎是異步的。 – VaporwareWolf

回答

0

內容大小打來電話,詢問代表們的高度後改變:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; 
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section; 
+2

感謝您的回覆。無論如何,我可以通知或任何類型的委託方法,會告訴我,內容的大小已經改變?它似乎發生在動畫完成後,但我希望得到新的內容大小之前,所以我可以在桌面周圍動畫相應 – SLEW

+0

我試着這樣做,並在我稱之爲tableview:heightForRowAtIndexPath:方法之前和之後放置日誌語句和沒有看到內容大小得到更新。但是,這確實有助於我找到解決方案來計算內容大小。我會將解決方案作爲單獨的答案發布。 – Daren

2

不幸的是,我一直沒能找到更新contentSize添加/從一個UITableView刪除行既可以當一個好方法,但如果您知道要添加/刪除的單元格的索引路徑,我的確找到了一種計算方法。

使用修改行的索引路徑,可以使用tableView:heightForRowAtIndexPath:方法計算各個單元格的高度,並將其添加到表視圖的當前內容大小高度。舉個例子,如果你只是添加一行:

[self.tableView insertRowsAtIndexPaths:@[indexPathOfNewRow] withRowAnimation:UITableViewRowAnimationAutomatic]; 
CGFloat newCellHeight = [self tableView:self.tableView heightForRowAtIndexPath:indexPathOfNewRow]; 
CGFloat newContentSizeHeight = self.tableView.contentSize.height + newCellHeight 
1

它看起來像sizeThatFits()可以公開的新contentSize說的待處理的高度。然後,您可以分配該待處理的大小,以便及早解決滾動動畫。

像這樣的東西:

extension UIScrollView { 

    var pendingContentSize: CGSize { 
     var tallSize = contentSize 
     tallSize.height = .greatestFiniteMagnitude 
     return sizeThatFits(tallSize) 
    } 

    func scrollToBottom(animated: Bool) { 
     contentSize = pendingContentSize 
     let contentRect = CGRect(origin: .zero, size: contentSize) 
     let (bottomSlice, _) = contentRect.divided(atDistance: 1, from: .maxYEdge) 
     guard !bottomSlice.isEmpty else { return } 
     scrollRectToVisible(bottomSlice, animated: animated) 
    } 

} 

我能寫的視圖控制器代碼:

tableView.insertRows(at: [newIndexPath], with: .none) 
tableView.scrollToBottom(animated: true) 

,並有表一路滾動到底部(使用新的內容大小)而不是滾動到倒數第二行(使用舊的內容大小)。

相關問題