Search code examples
iosxcodeuitableviewviewdidload

Call TableView:didSelectRow:AtIndexPath in viewDidLoad


I have to select a row in my UITableView after the view did load.

So I did:

 [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];

This works fine and the row is selected, but I also want the TableView:didSelectRow:AtIndexPath method to be called which is not the case for now.

Any ideas?

Thank you!


Solution

  • Since you know the row that you are selecting, you could simply call the method that gets called in tableView:didSelectRowAtIndexPath directly. Or you could call the tableView:didSelectRowAtIndexPath yourself:

    NSIndexPath *initialIndex = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView selectRowAtIndexPath:initialIndex
                                animated:YES 
                          scrollPosition:UITableViewScrollPositionTop];
    [self tableView:self.tableView didSelectRowAtIndexPath:initialIndex];