后编辑:prepareForSegue UISearchDisplayController
我才发现:How to segue from a UISearchBarDisplayController result to a 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];
}
}
}
}
这取自苹果的TableSearch示例。
原始问题是双重的:
1)self.tableView似乎不等于self.searchDisplayController.searchResultsTableView被prepareForSegue
2),其具有搜索的indexPath(行)总是0
我想我可以使用didSelectRow ...来代替或组合使用,但我相信准备...应该有可能?!另外,在尝试didSelectRow时...我确定如何将对象从相关行传递到目标控制器。也就是说,我能得到正确的indexPath在didSelectRow ...但我只知道如何在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];
}
任何帮助深表感谢。也许是一个教程的参考,它展示了如何组合这些东西。
谢谢!
哦。我的。天哪。你刚刚改变了我的生活。 我只是增加了一个数据ID参数来我的UITableViewCell子类(其已经进一步在我的项目子类为每一个细胞),而现在,如你所说,我并不需要担心那些恼人的索引路径或表视图比较了! 我真的希望我能不止一次地投票。再次感谢! – 2013-11-23 10:13:50