如何从UISearchBarDisplayController结果inheritance到detailViewController

所以,使用storyboard你可以创build一个从第一个tableViewController到detailViewController的UITableViewCell的segue。

但是,不要太复杂,当一个UISearchBarDisplayController被引入到故事板组合中时,如何将结果单元格放到detailViewController中?

我能够search没有问题,我遵循这个教程: http : //clingingtoideas.blogspot.com/2010/02/uitableview-how-to-part-2-search.html

我所能做的就是从search中select一行,它会变成蓝色,而不会进入detailViewController。

我已经实现了prepareForSegue方法,该方法适用于未search的单元格,但无法弄清楚这一点。

这是基于@James Chen评论的解决scheme。 还根据表所在的状态使用不同的IndexPath。

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"toDetail"]) { Person *person = nil; if (self.searchDisplayController.active == YES) { NSIndexPath *indexPath = indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow]; NSLog(@"segue - section: %d, row: %d", indexPath.section, indexPath.row); person = [self.filteredPersonArray objectAtIndex:indexPath.row]; } else { NSIndexPath *indexPath = indexPath = [self.tableView indexPathForSelectedRow]; NSLog(@"segue - section: %d, row: %d", indexPath.section, indexPath.row); person = [self.personArray objectAtIndex:indexPath.row]; } [[segue destinationViewController] setPerson:person]; } } 

我试过你的解决scheme,发现prepareForSegue被称为两次,由于生命周期,didSelect … – > performSegueWithIdentifier。

  1. self:prepareForSegue:目标控制器上的对象被设置(与错误的索引),因为
  2. dest:viewDidLoad:目标控制器视图在之后加载
  3. self:didSelectRow …:索引已知且正确设置。
  4. self:prepareForSegue:对象现在是正确的,但没有副作用。

然后,我专注于didSelect …并提出了这个解决scheme,我删除了segue并以编程方式推送视图:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { DestTableViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestViewController"]; CustomObj *custObj = nil; if (tableView == self.searchDisplayController.searchResultsTableView) { custObj = [filteredListContent objectAtIndex:indexPath.row]; } else { storeToDetail = [self.fetchedResultsController objectAtIndexPath:indexPath]; } controller.custObj = custObj; [self.navigationController setNavigationBarHidden:NO]; [self.navigationController pushViewController:controller animated:YES]; // presentViewController::animated:completion is always full screen (see problem below) } 

然后我遇到了一些问题,因为我遵循mapView中的segue,导致:

 //DestinationViewController - (IBAction)back:(id)sender { [self.navigationController popToRootViewControllerAnimated:YES]; // list [self.presentingViewController dismissViewControllerAnimated:YES completion:nil]; // map } 

这是不是做到这一点,但现在它的工作原理。

然而,第一部分是简单而干净的,也许它也适用于你?!

好吧,我想我知道了,它似乎有点破解,但它适用于我的目的:

我正在使用故事板:我有一个UITableview控制器与UISearchBarDisplayController直接在它上面。 没有代码只是拖放。

从那里,我按照这个教程来让search栏正确searchhttp://clingingtoideas.blogspot.com/2010/02/uitableview-how-to-part-2-search.html

然而prepareForSegue:只会让我显示一个单元格,而不是search数组。

所以我用didSelectRowAtIndexPath:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (savedSearchTerm) { searchRowSelected = indexPath.row; //<-- the trick that worked [self performSegueWithIdentifier:@"ShowDetail" sender:self]; } } 

searchRowSelected是我在类的顶部声明的一个intvariables。

didSelectRowAtIndexPath:知道我select哪一行,但是prepareForSegue没有。 那就是为什么我需要这个variables。

这就是我在prepareForSegue中使用它的原因:

 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if ([segue.identifier isEqualToString:@"ShowDetail"]) { dvc = [segue destinationViewController]; NSIndexPath* path = [self.tableView indexPathForSelectedRow]; int row = [path row]; if (savedSearchTerm){ //set the detailViewController with the searched data cell myDataClass* c = [searchResults objectAtIndex:searchRowSelected]; dvc.myDataClass = c; }else{ //set the detailViewController with the original data cell myDataClass* c = [array objectAtIndex:row]; dvc.myDataClass = c; } } } 

也使用这个代码来清理savedSearchTerm

 -(void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller{ [self setSavedSearchTerm:nil]; } 

如果有人有更好的解决scheme,我都耳朵:)