2
votes

I'm trying to add a search function to my tableview. I've started by creating the UITableView, which works perfect. The problem is when I start writing in the search bar I get this error:

Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<__NSCFString 0x8c44c00> valueForUndefinedKey:]:
this class is not key value coding-compliant for the key name.'

Then i've added the searchbar and searchdisplay to the viewcontroller.

searchdisplaycontroller outlets:

enter image description here

viewcontroller outlets:

enter image description here

in viewdidload

self.filteredArray = [NSMutableArray arrayWithCapacity:[finalArray count]];

numberOfRows method:

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

}

rest of the methods:

-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
    // Update the filtered array based on the search text and scope.
    // Remove all objects from the filtered search array
    [self.filteredArray removeAllObjects];
    // Filter the array using NSPredicate
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
    filteredArray = [NSMutableArray arrayWithArray:[finalArray     filteredArrayUsingPredicate:predicate]];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    // Tells the table data source to reload when text changes
    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller     shouldReloadTableForSearchScope:(NSInteger)searchOption {
    // Tells the table data source to reload when scope bar selection changes
    [self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}
1
Helped you to put your screen shots into your questions. and voted you up for giving you 5 reputations, so you can add image next time.Xu Yin
Thank you! i know the link was not optimal, but was the only solution i could find.user3258468

1 Answers

1
votes

The problem is with your predicate. If you see in the error message, it says this class is not key value coding-compliant for the key name.'

In your predicate,

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];

you're searching for the text on a property that doesn't exist (name). Without seeing more of your code or knowing more of what you're trying to accomplish, changing the predicate to say @"SELF contains..." instead of @"SELF.name contains..." should do the trick.

You can see the Apple docs on predicates or this article for more information.