为什么每当我使用搜索显示控制器时都会显示底层的UIViewTable

时间:2013-04-16 06:38:22

标签: ios objective-c xcode uitableview searchdisplaycontroller

我有一个UIViewController类,其中包含UITableViewSearchDisplayController。我面临的问题是,每当我点击搜索栏并输入查询时,它都会显示搜索结果并显示基础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的样子:

enter image description here

enter image description here

要清楚..显示的UI正如我想要的那样..但我只是为什么其他表正在进行所有这些渲染计算而感到困惑..这后来变成了一个真正的问题b / c它给出了我有些线程/异步代码令人头痛,而且这种背景和不必要的活动正​​在进行时很难调试。

注意:我也尝试过与IB不同的布局。我注意到的一件事是,如果我将搜索栏设置为tableView的相邻子视图,则会调用基础表甚至更多!

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

当我使用搜索时,有人能告诉我如何防止基础表格出现/被渲染吗?

1 个答案:

答案 0 :(得分:1)

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

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

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

我进一步注意到inspection表格视图询问其UITableViewDataSource的第一件事就是节的数量......之后发生了所有事情(即每个部分的行数和细胞的渲染)。所以我只是在部分检查部分返回0:

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

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