-1

I have a tableview list with clickable cells, when one is clicked a new viewcontroller opens up. When a back button is clicked and the first VC is called, the tableview resets to the top of the list. How can I change this so when the back button is clicked the tableview goes back to the original cell clicked? From what I understand, I need a tableview.scrollToRow, but I'm getting a little lost in the indexPath that I need to select (believe I need to save the last selected row, but now sure how to do this)

Here's the code:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let webVC = UIStoryboard.init(name: "MainVC", bundle: nil).instantiateViewController(withIdentifier: "SecondaryVC") as! WebViewController

    webVC.urlLink = self.listings?[indexPath.row].url

    self.present(webVC, animated: true, completion: nil) }


override func viewWillAppear(_ animated: Bool) {
    tableview.scrollToRow(at: indexPathSelected, at: .middle, animated: false)
}
AceVentura
  • 13
  • 4
  • Are you sure you are going back to the original instance of your view controller? If you are then you should not need to do anything; the view should be as it was before you moved to the new view controller. Can you show how you are going "back"? – Paulw11 Jun 15 '17 at 23:49
  • I think that may be the issue. In viewDidLoad i'm running a function that pulls json data for the tableView, and at the end of it I have DispatchQueue.main.async { self.tableview.reloadData()}. Maybe every time the VC is called, the data is reloading and the tableview is resetting to the top? I've setup the back button in the main.storyboard where I right clicked dragged and setup the first VC to "show". – AceVentura Jun 16 '17 at 20:04
  • As I suspected you are pushing a new instance of your view controller rather than going "back". Search for information on unwind segues and use one. You should also use a segue to to present MainVc rather than calling `present` directly – Paulw11 Jun 16 '17 at 22:36
  • Awesome, will check this out, thanks for the tip! – AceVentura Jun 17 '17 at 01:10
  • Unwind segue worked like a charm. you rock – AceVentura Jun 17 '17 at 01:57

2 Answers2

0

As your cell is respond to your touch(and you did not call deselectRow(at indexPath: IndexPath) after selecting cell), the tableView will keep your selection(s), so when you back to your viewController just call tableView.indexPathForSelectedRow(or tableView.indexPathsForSelectedRows) in your viewWillAppear() method.

Meonardo
  • 182
  • 13
  • Thanks for your reply, I'm working on this now. Let me get back to you and I'll let you know how it goes / if I have a follow-up question – AceVentura Jun 16 '17 at 20:07
  • Meonadro, @Paulw11 mentioned i should look at unwind segues which ended up working for me, cheers – AceVentura Jun 17 '17 at 01:58
0

Your problem is that you are actually moving forward to a new instance of your view controller rather than back to the existing instance. If you move back then the state of the view controller will be as you left it and there will be no need to do anything to the tableview.

You should use an unwind segue to move back

Paulw11
  • 108,386
  • 14
  • 159
  • 186