簡體   English   中英

如何以編程方式選擇uitableview單元格?

[英]How to select a uitableview cell programmatically?

我有一個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;

並通過傳遞indexPath在didSelectRowAtIndexPathsearchButtonClicked方法中對其進行了調用。 But the problem is that the cell doesn't get highlighted the way it gets highlighted when user clicks on it.

這對我有用,是正確的:

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方法中,您傳遞參數。

有效!!

-(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