在 UISearchDisplayController 之后准备ForSegue [英] prepareForSegue after UISearchDisplayController

查看:24
本文介绍了在 UISearchDisplayController 之后准备ForSegue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现:如何从UISearchBarDisplayController 结果到 detailViewController

我会看看!

我正在使用故事板将搜索栏和搜索显示控制器"与核心数据 fetchedResultsController 结合起来.也就是说,我必须区分:

I'm combining a 'search bar and search display controller' with core data fetchedResultsController using storyboard. That is, I have to differentiate between:

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

以及我刚刚列出从数据存储中获取的结果的情况.

and the case where I've just listed the results fetched from the data store.

但是,在以下情况下我无法获得正确的行(索引路径):

However, I'm having trouble getting the right row (index path) in the following case:

- (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 示例.

which is taken from Apple's TableSearch example.

原来的问题是双重的:

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

1) self.tableView doesn't seem to be equal to self.searchDisplayController.searchResultsTableView is prepareForSegue

2) 搜索过的 indexPath(行)始终为 0.

2) having searched the indexPath (row) is always 0.

我想我可以使用 didSelectRow... 代替或组合使用,但我相信 prepare... 应该是可能的?!此外,在试验 didSelectRow 时...我确定如何将对象从相关行传递到目标控制器.也就是说,我可以在 didSelectRow 中获得正确的 indexPath... 但我只知道如何在 preparFor... 中获取 segue 目标:

I suppose I could use didSelectRow... instead or in combination, but I believe prepare... should be possible?! Also, when experimenting with didSelectRow... I'm sure how to pass the object from the relevant row to the destination controller. That is, I can get the correct indexPath in didSelectRow... but I only know how to get the segue destination in the preparFor...:

- (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];
}

非常感谢任何帮助.也许是对展示如何组合这些内容的教程的参考.

Any help is much appreciated. Maybe a reference to a tutorial which shows how to combine theses things.

谢谢!

推荐答案

在这种情况下,prepareForSegue:sender:sender 参数设置为表视图单元格启动转场.

In this case the senderargument of prepareForSegue:sender: is set to the table view cell initiating the segue.

只需向您的单元格添加一个属性,该属性包含属于该单元格的 Store,您无需进行任何索引路径或表格视图比较.

Just add a property to your cells that holds the Store belonging to this cell and you don't have to do any of the index path or table view comparison dance.

你应该得到一些简单的东西

You should end up with something as simple as

- (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];
    }
}

这篇关于在 UISearchDisplayController 之后准备ForSegue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆