Richa Dua
Richa Dua

Reputation: 131

How to select a uitableview cell programmatically?

I have a UISearchController and i want to select the first row of the searchResultsController tableview on click of search button in the keyboard. For that I tried implementing

-(void)searchButtonClicked:(UISerachBar*)searchBar {
  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  [self.searchResultsController.tableView selectRowAtIndexPath: indexPath animated:false scrollPostion:UITableViewScrollPositionNone];
  [self.searchResultsController tableview:self.searchResultsController.tableView didSelectRowAtIndexPath: indexPath];
 }

but it didn't work for me!!

Then I tried to make a separate method:

-(void) plotPinOnMapWithIndexPath:(NSIndexPath*)indexpath;

and called it in didSelectRowAtIndexPath and in searchButtonClicked method by passing the indexPaths.But the problem is that the cell doesn't get highlighted the way it gets highlighted when user clicks on it.

Upvotes: 0

Views: 937

Answers (2)

Richa Dua
Richa Dua

Reputation: 131

It worked!!

-(void)searchButtonClicked:(UISerachBar*)searchBar {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self tableview:self.searchResultsController.tableView didSelectRowAtIndexPath: indexPath];

}

Upvotes: 0

Jiri Zachar
Jiri Zachar

Reputation: 587

This works for me, and is correct:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:NO     scrollPosition:UITableViewScrollPositionNone];

optionally try this:

[self.tableView:self.tableView didSelectRowAtIndexPath:indexPath];

or you can use Segue to continue with result:

[self performSegueWithIdentifier:"showDetailView" sender:self];

In prepareForSegue method you pass parameters....

Upvotes: 2

Related Questions