[英]How to stop UITableView scrolling to top after insert new row?
我在我的應用程序中使用了手風琴視圖,當我在UITableView
插入新行時,如果該部分在可見區域中,則一切正常。 但是,當我們更新下部的行時, UITableView
會自動滾動到頂部。 這里有什么問題? 誰能幫我解決這個問題?
[CATransaction begin];
[CATransaction setCompletionBlock:^{
NSLog(@"Completed");
}
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths: indexPathsToInsert withRowAnimation: UITableViewRowAnimationAutomatic];
[self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
[CATransaction commit];
有什么建議嗎? 我嘗試了scrollToRowAtIndexPath
, setContentOffSet
。
您是否正在使用自動布局讓tableview計算像元高度? 如果是,則必須將estimatedHeight
高度設置為接近盡快的實際高度。 如果不同單元格的高度不同:
func tableView(_ tableView: UITableView, estimatedHeightForRowAt
indexPath: IndexPath) -> CGFloat
之所以能使用“ accordian tutorial”,是因為它使用了固定的像元高度。
我沒有嘗試過,但是您可以嘗試以下操作:
self.tableView.scrollEnabled = NO
設置self.tableView.scrollEnabled = NO
,在更新之后將self.tableView.scrollEnabled = YES
self.tableView.scrollsToTop = NO
CGPoint
,然后在您進行更新調用后立即[self.tableView setContentOffset:previousPosition animated:NO]
animation [self.tableView setContentOffset:previousPosition animated:NO]
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.