简体   繁体   English

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

[英]How to stop UITableView scrolling to top after insert new row?

I have used accordian view in my app, While I insert new row in the UITableView , everything works good if the section is in visible area. 我在我的应用程序中使用了手风琴视图,当我在UITableView插入新行时,如果该部分在可见区域中,则一切正常。 But when we update lower section row, UITableView scrolls to top automatically. 但是,当我们更新下部的行时, UITableView会自动滚动到顶部。 Whats the issue here? 这里有什么问题? Can anyone help me to resolve this issue? 谁能帮我解决这个问题?

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

Any suggestion? 有什么建议吗? I tried scrollToRowAtIndexPath , setContentOffSet . 我尝试了scrollToRowAtIndexPathsetContentOffSet

Are you using autolayout to let the tableview calculate the cell height? 您是否正在使用自动布局让tableview计算像元高度? If yes, you have to set the estimatedHeight closed to the actual height of cell asap. 如果是,则必须将estimatedHeight高度设置为接近尽快的实际高度。 If you have different height for different cells: 如果不同单元格的高度不同:

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

"accordian tutorial" works because it uses fixed cell height. 之所以能使用“ accordian tutorial”,是因为它使用了固定的像元高度。

I didn't try but here's a list of things you can try : 我没有尝试过,但是您可以尝试以下操作:

  • Setting self.tableView.scrollEnabled = NO before your update and self.tableView.scrollEnabled = YES right after 在更新之前将self.tableView.scrollEnabled = NO设置self.tableView.scrollEnabled = NO ,在更新之后将self.tableView.scrollEnabled = YES
  • Not sure but you can try to set self.tableView.scrollsToTop = NO 不确定,但是您可以尝试设置self.tableView.scrollsToTop = NO
  • Save current position of scrollView in a CGPoint , then right after your update call [self.tableView setContentOffset:previousPosition animated:NO] 将scrollView的当前位置保存在CGPoint ,然后在您进行更新调用后立即[self.tableView setContentOffset:previousPosition animated:NO] animation [self.tableView setContentOffset:previousPosition animated:NO]

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM