0

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.

1 Answers1

0

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

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • How Can I do that? I looking for some examples but i do not find it. – Diego Rodrigues Feb 21 '14 at 19:21
  • I don't know because I don't understand what you are trying to do. But look for example at this code: https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch08p438searchableTable/ch21p718sections/RootViewController.m Look at the `filterData:` implementation to see how I form the filtered array for my table based on what the user types in the search field. – matt Feb 21 '14 at 20:34
  • What I want to do is filter the NSArray _rotulos (that populates my TableView) store this filtered array in my NSArray _resultadosBusca and Reload my TableView with this new data. – Diego Rodrigues Feb 21 '14 at 20:58
  • I know. But I do not know on what _basis_ you want to filter that NSArray. However, I don't need to, because I'm not going to write your code for you. _Look at the code I pointed you at._ I am showing you exactly how this is done. You should easily be able to adapt that example. – matt Feb 21 '14 at 21:03