0

I have implemented didSelectRowAtIndexPath in a UITableViewController:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    // determine selected cell
    let name = self.names[indexPath.row]

    // present menu VC
    let destVC: UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("MenuItemsReadOnly") as UIViewController

    self.navigationController?.pushViewController(destVC, animated: true)
}

So far so good. It works fine. My problem starts right after I dismiss the destVC:

func doneSelf() {
    self.navigationController?.popViewControllerAnimated(true)
}

Following dismissal, I am back to my UITableViewController, but the view scrolls all the way up to the top so that the 1st cell is visible.

I have not implemented function viewDidAppear. I am not calling tableView.reloadData() in viewDidLoad, and this is the only view-related method I have implemented in my UITableViewController:

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.delegate = self
    self.tableView.dataSource = self

    // format navigation controller
    self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
    self.navigationController!.navigationBar.translucent = false
    self.navigationController!.navigationBar.barStyle = .Black
    self.navigationController!.navigationBar.barTintColor = UIColor.flatRedColor()
    self.navigationController!.navigationBar.tintColor = UIColor.flatRedColor()

    self.automaticallyAdjustsScrollViewInsets = false

    self.tableView.backgroundColor = UIColor.flatWhiteColor()

    // format table UI
    self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None

    // last cell would get cropped off without this
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0)

    // navigation buttons
    navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "IconMenuShortcut"), style: .Plain, target: self, action: "presentLeftMenuViewController")
    navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()        
}

Is this by design? How can I keep the selected cell into view once I am back from the 2nd UIViewController and not automatically move back to the top?

ppp
  • 713
  • 1
  • 8
  • 23
  • That is not by design, you probably have written some code in `viewDid/WillAppear` which causes that. – luk2302 Jan 22 '16 at 10:07
  • 1
    http://stackoverflow.com/questions/34868661/how-to-stop-uitableview-from-returning-to-top-cell-after-popover/34868986#34868986 Use this! It will solve your problem. Kindly upvote the answer as well. – rushisangani Jan 22 '16 at 18:58
  • This works great, thanks! If you rewrite your comment as an answer I will accept it. – ppp Jan 22 '16 at 19:15

1 Answers1

0

Have a reference on the selected indexPath selected and when comeback to the TableviewController, call

    [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:YES];
AskIOS
  • 918
  • 7
  • 19
  • Thanks for the suggestion but there's still some movement with this, even with animated: false option. What I need is for the tableView not to move at all after the 2nd VC is dismissed. – ppp Jan 22 '16 at 18:36