0

我目前正在尝试向我的应用程序添加一些搜索功能,但是在配置时存在一些问题。我有一个带有 3 个表视图的基于选项卡的应用程序,我想为所有表视图添加一个搜索栏。但设置起来似乎并不容易。

出现的第一个问题是,在 Storyboard-Editor 中,我只能为每个 tableview 添加单独的搜索栏,但无法将搜索栏添加到 tabBarController 本身。这样相同的搜索栏在所有 3 个表格视图中都可见。

那么第二个问题是,如果我知道它是如何工作的,我必须设置一个带有 3 个不同表视图的 searchDisplayController,但我可以只用一个表视图初始化一个 searchDisplayController。

在 iPhone 上使用一个搜索栏搜索 3 个不同类别的最佳方法是什么?那里有教程吗?我也在看一些其他的应用程序,比如 facebook,他们也在一个 tableview 中搜索。

4

2 回答 2

1

您可以使用三个搜索显示控制器,如下所示:让每个选项卡上的每个 vc 实现一个方法(并公开声明),如下所示:

- (void)searchFor:(NSString *)string {
    [self.searchDisplayController setActive:YES];
    self.searchDisplayController.searchBar.text = string;
}

每个都应该实现这个方法:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {

    // do the search logic for my table
    // set a badge on my tab indicating how many results I found

    // then perform it on the other vcs:
    NSMutableArray *otherVCs = [[self.tabBarController viewControllers] mutableCopy];
    [otherVCs removeObject:self];

    for (MyViewController *otherVC in otherVCs) {
        [otherVC searchFor:searchString];
    }
}

您可能需要确保已加载所有其他选项卡 vcs(如果您启动应用程序,请仅访问一个选项卡并尝试此操作,其他 vcs 可能尚未准备好。为此,只需在循环中插入此行以强制查看加载: (void)[otherVC view];

(注意 - 这个答案假定 ARC)

于 2013-01-19T18:50:17.550 回答
0

这个解决方案有一点问题。它会导致无限循环。解决方案是我们必须证明视图是否可见。

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self setUpFilteredEndlessScrollingWithUrl:kGetAllTours];
    [self loadFilteredDataWithPath:kGetAllTours];
    if (self.isViewLoaded && self.view.window) {
        NSMutableArray *otherVCs = [[self.tabBarController viewControllers] mutableCopy];
        [otherVCs removeObject:self];

        for (BaseTableViewController *otherVC in otherVCs) {
            [otherVC searchFor:searchString];
        }
    }
    return YES;
}
于 2013-01-23T12:46:14.470 回答