我有一段时间让整个Core Data,Storyboard,UISearchBar三人组合在一起工作。最后用Core Data成功构建了表,用搜索文本缩小了项目,并修改了prepareForSegue,还有一个打嗝......
当我点击表格中的任何项目进入详细视图时,未经过滤的表格中的一切都很好。调用PrepareForSegue并完美显示细节。
当我搜索时,我的表被过滤了(我现在要过滤数组选项而不是第二个NSFetchedResultsController,但不是因为没有尝试!)。
当我点击过滤列表中的某个项目时,会调用prepareForSegue并按下详细信息视图,但是,它总是从列表中的第一个项目中提取详细信息!
例如,如果我搜索“c”并且列表缩小为“Charlie”和“Cookie”,当我选择“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;
任何帮助将不胜感激!
答案 0 :(得分:12)
感谢Phillip Mills,答案是:
只需添加:
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;