How to stop UITableView from returning to top cell after popover

南笙酒味 提交于 2019-12-02 07:12:06

Get tableview content offset before displaying your zoomViewController.

-(void)viewWillDisappear:(BOOL)animated {

  [super viewWillDisappear:animated];

  tableViewContentOffset = self.tableView.contentOffset;

}

and set tableview offset again in viewWillAppear

-(void)viewWillAppear:(BOOL)animated {

   [super viewWillAppear:animated];

   [self.tableView setContentOffset:tableViewContentOffset];
 }

most probably the reason for this behavior is that you are calling table reload some where between opening and closing the popover view (probably inside one or more of these methods like viewDidAppear, viewWillAppear etc). If you can simply remove it then it wont be scrolling to top.

or you can keep the content offset just before opening popover view then set the offset after closing it,

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    tableView.contentOffset = offset
}

override func viewDidDisappear(animated: Bool) {
    super.viewDidDisappear(animated)
    offset = tableView.contentOffset
}

please check, if you are calling reloadData in viewWillAppear or viewDidAppear. or if you are calling any reloadsection in either of these 2 methods.

You can also validate these calls also in viewWillDisappear/viewDidDisappear methods too.

Cache selected indexPath of the selected image cell, and in viewWillAppear method,

    if(selectedPath != nil) {
        [self.tableView scrollToRowAtIndexPath:selectedPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
     }

This will scroll back to your selected image row.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!