1

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);
    });
rmaddy
  • 314,917
  • 42
  • 532
  • 579
rohaldb
  • 589
  • 7
  • 24

4 Answers4

6

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];
 }
rushisangani
  • 3,195
  • 2
  • 14
  • 18
  • This is a great solution! However I am assuming this is objective C? Here is what i put (Swift) var tableViewContentOffset = CGPointMake(0, 0) override func viewWillDisappear(animated: Bool) { tableViewContentOffset = self.tableView.contentOffset; } override func viewWillAppear(animated: Bool) { self.tableView.contentOffset = tableViewContentOffset } – rohaldb Jan 19 '16 at 08:04
  • Thank You for such a great solution. :) –  Jan 19 '16 at 10:44
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
}
RJE
  • 781
  • 6
  • 14
1

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.

Ankit Thakur
  • 4,739
  • 1
  • 19
  • 35
0

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.

AskIOS
  • 918
  • 7
  • 19