简体   繁体   English

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

[英]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. 我有一个UISearchController,我想在键盘上单击搜索按钮时选择searchResultsController tableview的第一行。 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. 并通过传递indexPath在didSelectRowAtIndexPathsearchButtonClicked方法中对其进行了调用。 But the problem is that the cell doesn't get highlighted the way it gets highlighted when user clicks on it.

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: 或者您可以使用Segue继续执行结果:

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

In prepareForSegue method you pass parameters.... 在prepareForSegue方法中,您传递参数。

It worked!! 有效!!

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM