如何以编程方式选择uitableview单元格?

时间:2015-07-03 10:19:55

标签: ios objective-c uitableview uisearchcontroller

我有一个UISearchController,我想在点击键盘上的搜索按钮时选择searchResultsController表视图的第一行。 为此,我尝试实施

-(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];
 }

但它对我没用!!

然后我尝试制作一个单独的方法:

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

并通过传递indexPaths在didSelectRowAtIndexPathsearchButtonClicked方法中调用它。But the problem is that the cell doesn't get highlighted the way it gets highlighted when user clicks on it.

2 个答案:

答案 0 :(得分:2)

这对我有用,而且是正确的:

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

可选择尝试:

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

或者您可以使用Segue继续结果:

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

在prepareForSegue方法中,您传递参数....

答案 1 :(得分:0)

有效!!

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

}