Search code examples
iosuitableviewcellseguenull

Preselect a UITableViewCell from a segue


I am using Storyboards and have a viewcontroller transition from one in which I want to pre-select a cell of a UITableView in the destination view controller.

prepareForSeque in the source viewcontroller passes a string value into a NSString of the destination viewcontroller.

What I want to do is on loading the destination viewcontroller, have the UITableView load with the cell that matches the string value that was passed via the segue call...

Problem is - the UITableView is nil when I try to access it in viewDidLoad of my destination viewcontroller...I wanted to make a call to selectRowAtIndexPath from there...

thanks in advance!


Solution

  • In prepare for segue, you can reference the destinationViewController and make assignments or other calls to it.

    I would recommend doing an

    @property (nonatomic, strong) NSString *yourCellId;
    

    in your viewController.h

    Then in prepare to segue, you can so a [[segue destinationViewController] setYourCellId:yourstring] and it will set that value.

    Then in viewDidLoad you can access yourCellId, as it will be set after the init, and before viewDidLoad