编辑:
我刚刚发现:如何从 UISearchBarDisplayController 结果到 detailViewController
我来看看!
我正在使用情节提要将“搜索栏和搜索显示控制器”与核心数据 fetchedResultsController 相结合。也就是说,我必须区分:
if (self.tableView == self.searchDisplayController.searchResultsTableView) {
...
以及我刚刚列出从数据存储中获取的结果的情况。
但是,在以下情况下,我无法获得正确的行(索引路径):
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"StoreDetails"]) {
UINavigationController *navigationController = segue.destinationViewController;
StoreDetailsTableViewController *controller = (StoreDetailsTableViewController *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
Store *storeToDetail = nil;
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; // This gives wrong row when having searched using searchDisplayController
NSLog(@"Row: %i", indexPath.row); // row is always 0
if (self.tableView == self.searchDisplayController.searchResultsTableView) {
storeToDetail = [self.filteredListContent objectAtIndex:indexPath.row];
} else {
storeToDetail = [self.fetchedResultsController objectAtIndexPath:indexPath];
}
controller.storeToDetail = storeToDetail;
}
}
之后调用:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:@"All"];
...
和:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.filteredListContent removeAllObjects];
for (Store *store in [self.fetchedResultsController fetchedObjects])
{
if ([scope isEqualToString:@"All"] || [store.name isEqualToString:scope])
{
NSComparisonResult result = [store.name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
[self.filteredListContent addObject:store];
}
}
}
}
取自 Apple 的 TableSearch 示例。
最初的问题是双重的:
1) self.tableView 似乎不等于 self.searchDisplayController.searchResultsTableView 是 prepareForSegue
2) 已搜索 indexPath (row) 始终为 0。
我想我可以使用 didSelectRow... 代替或组合使用,但我相信 prepare... 应该是可能的?!此外,在尝试使用 didSelectRow 时...我确定如何将对象从相关行传递到目标控制器。也就是说,我可以在 didSelectRow... 中获得正确的 indexPath... 但我只知道如何在 preparFor... 中获得 segue 目的地
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Store *storeToDetail = nil;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
storeToDetail = [self.filteredListContent objectAtIndex:indexPath.row];
}
else
{
storeToDetail = [self.fetchedResultsController objectAtIndexPath:indexPath];
}
// How to get segue destination controller and set object?!
// Before doing:
[self performSegueWithIdentifier:@"StoreDetails" sender:self];
}
任何帮助深表感谢。也许是对展示如何组合这些东西的教程的参考。
谢谢!