如何在UISearchDisplayController的searchResultTableView中覆盖"无结果"文本?

JoJ*_*oJo 5 iphone cocoa-touch uisearchdisplaycontroller

我的服务器正在处理搜索查询时,我不想显示"无结果"文本.

在此输入图像描述

我想出了包含标签的表格单元格的确切坐标,并试图覆盖它.

self.noResultsCoverView = [[[UIView alloc] initWithFrame:CGRectMake(
    0.0, 
    44.0, 
    320.0, 
    43.0
)] autorelease];
self.noResultsCoverView.backgroundColor = [UIColor whiteColor];
[self.searchDisplayController.searchResultsTableView addSubview:self.noResultsCoverView];
Run Code Online (Sandbox Code Playgroud)

令我懊恼的是,我的封面位于桌面视图上方,但位于标签下方.我需要盖子在标签上方.searchResultsTableView::bringSubviewToFront没有用,这让我觉得这个标签根本就不是孩子searchResultsTableView.

顺便说一下,这个Stack Overflow的答案对我来说并不适用.它适用于第一次搜索,但在后续搜索中闪烁一个奇怪的黑色封面.

小智 20

这应该做得很好.返回至少一个单元格的代码:

BOOL ivarNoResults; // put this somewhere in @interface or at top of @implementation
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        if (filteredList.count == 0) {
            ivarNoResults = YES;
            return 1;
        } else {
            ivarNoResults = NO;
            return [filteredList count];
        }
    }
    // {…}
    // return the unfiltered array count
}
Run Code Online (Sandbox Code Playgroud)

并且"显示"干净的细胞:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if (tableView == self.searchDisplayController.searchResultsTableView && ivarNoResults) {
        static NSString *cleanCellIdent = @"cleanCell";
        UITableViewCell *ccell = [tableView dequeueReusableCellWithIdentifier:cleanCellIdent];
        if (ccell == nil) {
            ccell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cleanCellIdent] autorelease];
            ccell.userInteractionEnabled = NO;
        }
        return ccell;
    }

    // {…}
}
Run Code Online (Sandbox Code Playgroud)


Fel*_*lix 6

解决此问题的最简单方法是在查询正在进行时在numberOfRowsInSection中返回1并将虚拟单元格保留为空或将其隐藏属性设置为YES以使其不可见.