我有一段时间让整个核心数据,故事板,UISearchBar三重奏一起工作。终于成功地用核心数据构建了表格,用搜索文本缩小了项目范围,并修改了 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;
任何帮助将不胜感激!
感谢菲利普·米尔斯的回答:
只需添加:
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
完整示例:
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;