2

后编辑: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]; 
} 

任何帮助深表感谢。也许是一个教程的参考,它展示了如何组合这些东西。

谢谢!

回答

1

- (无效)prepareForSegue:(UIStoryboardSegue *)赛格瑞发件人:(ID)发送方

{

如果(self.filteredListContent)

{//的NSLog(@“后滤波的数据“;}

其他

{//的NSLog(@” 后的所有“;}

}

4

在这种情况下sender参数prepareForSegue:sender:被设置为启动segue的表格视图单元格。

只需添加一个属性,你的细胞保持Store属于该单元格,你不必做任何索引路径或表视图比较舞蹈。

您应该结束了与作为

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    FooCell *cell = (FooCell *)[self.tableView dequeueReusableCellWithIdentifier:CELL_IDENTIFIER]; 
    Foo *foo = [self fooForIndexPath:indexPath]; 

    cell.foo = foo; 
    // additional initialization 

    return cell; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"view detail"]) { 
     DetailViewController *destViewController = segue.destinationViewController; 
     destViewController.foo = [(FooCell *)sender foo]; 
    } 
} 
+0

哦。我的。天哪。你刚刚改变了我的生活。 我只是增加了一个数据ID参数来我的UITableViewCell子类(其已经进一步在我的项目子类为每一个细胞),而现在,如你所说,我并不需要担心那些恼人的索引路径或表视图比较了! 我真的希望我能不止一次地投票。再次感谢! – 2013-11-23 10:13:50

0

您使用了错误的测试一样简单。

if (self.tableView == self.searchDisplayController.searchResultsTableView) { 
    ... 

这总是会失败,你问是否两个不同的tableView是不同的。答案总是否定的。

但是,您可以测试发件人是否来自searchResultsTableView。

NSIndexPath *searchIndexPathOfSelectedCell = [self.searchDisplayController.searchResultsTableView indexPathForCell:sender]; 

if (searchIndexPathOfSelectedCell) { 
    ...