Frage

I'm trying to implement a search bar for my iOS 7 app.

The table view doesn't show the values when the filter is active, but the filter is correct.

So, I have all the results: enter image description here

Then, I started to filter the data with no valid result:

enter image description here

Finally, I used a valid filter and log result is correct, but the table doesn't show this: enter image description here

I don't know how to find problem. My code:

- (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;
}
War es hilfreich?

Lösung

@Danh has given the right answer in his comment. The problem here is that you are saying:

cell.razonSocialLabel.text = balance.razonSocial;

But what if cell is not actually a BalancesTableViewCell? Then cell.razonSocialLabel is nil, cell.razonSocialLabel.text call setText: on nil, and nothing happens. So you are getting cells, all right, but they are all displayed as blank.

You need to obtain your cells from your real table; that is the table that hands out BalancesTableViewCell when you dequeue a cell. But instead you are obtaining your cells from tableView, which in the case of the filtered table is the search display controller's table view, which knows nothing of BalancesTableViewCell.

Thus, as Danh rightly says, you must change this line:

BalancesTableViewCell *cell = 
    (BalancesTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

to this:

BalancesTableViewCell *cell = 
    (BalancesTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top