搜索栏工作正常,但表格不显示结果

bub*_*drc 1 iphone uitableview ios searchbar

我正在尝试为我的iOS 7应用程序实现搜索栏.

表格视图不显示过滤器处于活动状态时的值,但过滤器是正确的.

所以,我得到了所有结果: 在此输入图像描述

然后,我开始过滤数据没有有效的结果:

在此输入图像描述

最后,我使用了一个有效的过滤器,并且日志结果是正确的,但表中没有显示: 在此输入图像描述

我不知道如何找到问题.我的代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [self.searchResults count];
    } else {
        return [self.balancesData count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
    static NSString *CellIdentifier = @"balancesCell";
    BalancesTableViewCell *cell = (BalancesTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    if (cell == nil) {
        cell = [[BalancesTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:CellIdentifier];
    }

    Balances *balance = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        balance = self.searchResults[indexPath.row];
    } else {
        balance = self.balancesData[indexPath.row];
    }

    cell.razonSocialLabel.text = balance.razonSocial;
    cell.importeLabel.text = balance.importe;

    tableView.backgroundColor =  cell.backgroundColor = [UIColor colorWithRed: 0.937 green: 0.937 blue: 0.957 alpha: 1.0];

    return cell;
}

    - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"razonSocial contains[c] %@", searchText];
    self.searchResults = [self.balancesData filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller     shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar     scopeButtonTitles]
                                      objectAtIndex:    [self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}
Run Code Online (Sandbox Code Playgroud)

mat*_*att 6

@Danh在评论中给出了正确答案.这里的问题是你在说:

cell.razonSocialLabel.text = balance.razonSocial;
Run Code Online (Sandbox Code Playgroud)

但如果cell竟BalancesTableViewCell?然后cell.razonSocialLabel是零,cell.razonSocialLabel.text打电话setText:给零,没有任何反应.所以你得到了细胞,好吧,但它们都显示为空白.

你需要从你的真实桌子中获取你的细胞; 这是在您出列单元格时将BalancesTableViewCell分发的表格.但是你要获取你的单元格tableView,在过滤表格的情况下是搜索显示控制器的表视图,它对BalancesTableViewCell一无所知.

因此,正如Danh所说,你必须改变这一行:

BalancesTableViewCell *cell = 
    (BalancesTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Run Code Online (Sandbox Code Playgroud)

对此:

BalancesTableViewCell *cell = 
    (BalancesTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Run Code Online (Sandbox Code Playgroud)