Question

I had a TableView which worked perfectly. Now I have included a search in TableView with the SearchBar component and Display ViewController. I want the search to be made based on the label that is populated by NSArray _rotulos.

@property (nonatomic, strong) NSArray *rotulos;

I fill the content in my viewDidLoad method (there is more objects, but here is just an example)

_rotulos = @[@"Item 1", @"Item 2", @"Item 3", @"Block 1", @"Block 2",@"Block 3"];

I populate in my cellForRowAtIndexPath method

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

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

    long row = [indexPath row];

    cell.rotuloLabel.text    = _rotulos[row];
    cell.descricaoLabel.text = _vinicola[row];
    cell.uvaLabel.text       = _uva[row];

    return cell;
}

This works fine. I have the problem when a click in the SearchBar and start typing.

Here is the code that make this search.

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

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

    return YES;
}

I have the problem in the line NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"_rotulos contains[c] %@", searchText]; This is my output Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFConstantString 0x243c8> valueForUndefinedKey:]: this class is not key value coding-compliant for the key _rotulos.'

I follow this Tutorial but in my case _rotulos is not a property of some classe, is a NSArray.

How i fix that? Sorry if a forget to post something.

Was it helpful?

Solution

You can't form this filter using predicateWithFormat:. Use predicateWithBlock: or create the filtered array in some other way (i.e. not filteredArrayUsingPredicate:).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top