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

标签 ios objective-c uitableview uisearchcontroller

我有一个 UISearchController,我想在单击键盘上的搜索按钮时选择 searchResultsController tableview 的第一行。 为此,我尝试实现

-(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 方法中调用它。但问题是单元格没有像它获取的那样突出显示当用户点击它时突出显示。

最佳答案

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

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 方法中你传递参数....

关于ios - 如何以编程方式选择 uitableview 单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31204365/

相关文章:

ios - UITableView 重新加载数据很慢

iphone - 当我将我的应用程序提交到 Apple 应用程序商店时,他们会看到我的源代码吗?

ios - 第一个单元格 UItableViewController 问题 Swift

ios - 让用户拍照并在表格 View 中使用它们的最佳做法是什么?

ios - 无法使用动态数量的节和行向 UITableView 插入新行

javascript - iOS亚麻布背景CSS?

ios - 从 iOS 共享扩展打开包含应用程序

ios - segue 中的目标 View 给出了属性错误

objective-c - 如何在执行 popToRootViewController 后将 View Controller 推送到导航堆栈

iOS 如何制作图书应用程序?导航 Controller + 文本 + 索引