6

我有一段时间让整个 Core Data、Storyboard、UISearchBar 三重奏按应有的方式一起工作。终于用 Core Data 成功建表,用 Search Text 缩小项目范围,并修改了 prepareForSegue,还有一个小问题......

当我单击表中的任何项目以转到详细信息视图时,未过滤表中的一切都很好。PrepareForSegue 被调用,细节完美呈现。

当我搜索时,我的表被过滤(我现在要过滤数组选项而不是第二个 NSFetchedResultsController,但不是因为缺乏尝试!)。

当我单击过滤列表中的项目时,会调用 prepareForSegue 并推送详细信息视图,但是,它总是从列表中的第一项中提取详细信息!

例如,如果我搜索“c”并且列表缩小到“Charlie”和“Cookie”,当我选择“Charlie”时,我会看到“Charlie”的详细视图。当我选择“Cookie”时,不幸的是,我还看到了“查理”的详细视图

我假设 prepareForSegue 代码是问题所在(可能是错误的?)。这是代码:

    SampleTVC *sampleDetailTVC = segue.destinationViewController;
    sampleDetailTVC.delegate = self;

    // Store selected Role in selectedRole property
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
  //  self.selectedRole = [self.fetchedResultsController objectAtIndexPath:indexPath];
    if (savedSearchTerm){
        self.selectedRole = [self.searchResults objectAtIndex:indexPath.row];
    } else {
        self.selectedRole = [self.fetchedResultsController objectAtIndexPath:indexPath];
    }
    NSLog(@"Passing selected role (%@) to SampleTVC", self.selectedRole.name);
    sampleDetailTVC.role = self.selectedRole;

任何帮助,将不胜感激!

4

1 回答 1

12

Thanks to Phillip Mills, for the answer:

just simply had to add:

    indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];

full sample:

    SampleTVC *sampleDetailTVC = segue.destinationViewController;
    sampleDetailTVC.delegate = self;
     // Store selected Role in selectedRole property
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    //  self.selectedRole = [self.fetchedResultsController objectAtIndexPath:indexPath];
    if (savedSearchTerm){
         indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
    self.selectedRole = [self.searchResults objectAtIndex:indexPath.row];
    } else {
        self.selectedRole = [self.fetchedResultsController objectAtIndexPath:indexPath];
     }
    NSLog(@"Passing selected role (%@) to SampleTVC", self.selectedRole.name);
    sampleDetailTVC.role = self.selectedRole;
于 2012-05-31T21:28:58.437 回答