在UISearchDisplayController之后准备ForSegue

时间:2012-04-05 17:38:38

标签: storyboard didselectrowatindexpath searchdisplaycontroller

编辑:

我刚刚找到: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];
            }
        }
    }
}

取自Apple的TableSearch示例。

最初的问题是双重的:

1)self.tableView似乎不等于self.searchDisplayController.searchResultsTableView是prepareForSegue

2)搜索到indexPath(行)始终为0。

我想我可以使用didSelectRow代替或组合使用,但我相信准备......应该可以吗?!此外,在尝试使用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];
}

非常感谢任何帮助。也许是对教程的参考,该教程展示了如何组合这些东西。

谢谢!

3 个答案:

答案 0 :(得分:4)

在这种情况下,sender的{​​{1}}参数设置为启动segue的表视图单元格。

只需向包含属于此单元格的prepareForSegue:sender:的单元格添加属性,您就不必执行任何索引路径或表格视图比较舞蹈。

你应该得到像

这样简单的东西
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;
}

答案 1 :(得分:1)

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

如果(self.filteredListContent)

{// NSLog(@"过滤后的数据&#34 ;;}

否则

{// NSLog(@" post all&#34 ;;}

}

答案 2 :(得分:0)

您使用的是错误的测试。

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

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

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

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

if (searchIndexPathOfSelectedCell) {
  ...