簡體   English   中英

插入新行后如何停止UITableView滾動到頂部?

[英]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];

有什么建議嗎? 我嘗試了scrollToRowAtIndexPathsetContentOffSet

您是否正在使用自動布局讓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
  • 將scrollView的當前位置保存在CGPoint ,然后在您進行更新調用后立即[self.tableView setContentOffset:previousPosition animated:NO] animation [self.tableView setContentOffset:previousPosition animated:NO]

希望這可以幫助。

暫無
暫無

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

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