How to stop UITableView from returning to top cell after popover

风流意气都作罢 提交于 2019-12-02 13:23:18

问题


I have a tableView full of images. When a user taps on an image (within the cell), a viewController with a larger zoomable version of the image is called with a popover segue. When i dismiss the popover and return to my tableView, it automatically displays the top cell of the table. Is there any way to stop this happening, and return to the previous position/cell before the popover was called?

EDIT: To call the popover i use

performSegueWithIdentifier("segueToPopOver", sender: tap.view)

to dismiss it i use the following:

 let tmpController :UIViewController! = self.presentingViewController;


 self.dismissViewControllerAnimated(true, completion: {()->Void in
        tmpController.dismissViewControllerAnimated(true, completion: nil);
    });

回答1:


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



回答2:


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
}



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/34868661/how-to-stop-uitableview-from-returning-to-top-cell-after-popover

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