如何在插入新行后停止UITableView滚动到顶部?

时间:2016-06-21 12:40:47

标签: ios uiscrollview tableview catransition

我在我的应用程序中使用了手风琴视图,当我在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

2 个答案:

答案 0 :(得分:3)

您是否使用autolayout让tableview计算单元格高度?如果是,则必须尽快将estimatedHeight设置为单元格的实际高度。如果不同的细胞有不同的高度:

func tableView(_ tableView: UITableView, estimatedHeightForRowAt
indexPath: IndexPath) -> CGFloat

“accordian tutorial”有效,因为它使用固定的单元格高度。

答案 1 :(得分:0)

我没有尝试,但这里列出了你可以尝试的事项:

  • 在更新前设置self.tableView.scrollEnabled = NO,在
  • 之后设置self.tableView.scrollEnabled = YES
  • 不确定,但您可以尝试设置self.tableView.scrollsToTop = NO
  • CGPoint中保存scrollView的当前位置,然后在更新通话后[self.tableView setContentOffset:previousPosition animated:NO]

希望这有帮助。