Eric Hartner
Eric Hartner

Reputation: 1

Trying To Call selectRowAtIndexPath Of My searchController.searchResultsTableView

What I'm trying to do is call selectRowAtIndexPath in my searchResultsTableView. I am doing this in my viewWillAppear so that the row gets selected after it comes back from another view controller. In this simple example, I'm just asking it to go to row 2. What happens is that it briefly selects row 2 and then the selection goes away. Any ideas?

-(void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // just select row 2 as a test
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:0];
    [self.searchController.searchResultsTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    [self.searchController.searchResultsTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:NO];
}

Thanks

Upvotes: 0

Views: 2792

Answers (2)

Wasim
Wasim

Reputation: 1349

Just go with viewDidLoad or viewDidAppear, and just use the single line told by EmilioPelaez.

Upvotes: 0

EmilioPelaez
EmilioPelaez

Reputation: 19882

Maybe you should call it in viewDidAppear:, so it's selected once the view is present, and not just before it's presented. The superclass of your controller might deselect all rows on viewDidAppear: (I'm not sure of that, though).

Also, I think you can do those two lines in just one:

[self.searchController.searchResultsTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionTop];

Upvotes: 2

Related Questions