0

我有一个UIViewController包含 aUITableView和 a的类SearchDisplayController。我面临的问题是,每当我单击搜索栏并键入查询时,它都会显示搜索结果并出于某种原因显示底层 UITableView。

例如,如果我在运行搜索时运行此代码:

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        ..
        NSLog(@"constructing a search result table view cell ");            
    } else
    {
        ..
        NSLog(@"::: constructing a mail box  table view cell");
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        NSLog(@"searchDisplayController: number of rows: %d", [queryResultData count]);
    } else {
        NSLog(@"mailbox: NUMBER OF ROWS: %d", [self.emailData count]);
    }
}

日志将如下所示:

[5033:c07] performing search with query hi
[5033:c07] ftSearch started with query = hi dbNum = 0
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] searchDisplayController: number of rows are 1
[5033:c07] ::: constructing a search result table view cell 
[5033:c07] searchDisplayController: number of rows are 2
[5033:c07] ::: constructing a search result table view cell 
[5033:c07] searchDisplayController: number of rows are 3
[5033:c07] ::: constructing a search result table view cell 
[5033:c07] searchDisplayController: number of rows are 4
[5033:c07] ::: constructing a search result table view cell 
[5033:c07] searchDisplayController: number of rows are 5
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19
[5033:c07] searchDisplayController: number of rows are 6
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell

这就是 IB 的样子:

在此处输入图像描述

在此处输入图像描述

要清楚.. 显示的 UI 正是我想要的.. 但我只是对为什么另一个表正在执行所有这些渲染计算感到困惑.. 这后来成为一个真正的问题 b/c 它给了我一些线程/asynchronous 代码令人头疼,在这种背景和不必要的活动发生时很难调试。

注意:我还尝试了 IB 的不同布局。我注意到的一件事是,如果我将搜索栏设置为 tableView 的相邻子视图 .. 基础表将被调用更多!

我还考虑过创建一个标志并检查搜索栏当前是否正在使用,然后基本上阻止底层的 UITableView 做任何工作..但这还不够好..它不应该首先被调用.

有人可以告诉我在使用搜索时如何防止基础表出现/呈现吗?

4

1 回答 1

1

这并没有直接解决我的担忧..但它在一定程度上限制了损害。我创建了一个存储搜索是否处于活动状态的局部变量:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    isSearchViewDisplayed = YES;
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    isSearchViewDisplayed = NO;
}

经过进一步检查,我注意到tableview 询问它的第一件事UITableViewDataSource是节数..一切(即每节的行数和单元格的渲染)都在此之后发生..所以我只是在节检查部分返回 0:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView == self.searchDisplayController.searchResultsTableView 
           || !isSearchViewDisplayed) {
        return 1;
    } else {
        return 0;
    }
}

我不会将此标记为正确.. 只是等待看看是否有更好的答案。

于 2013-04-16T07:24:54.690 回答