UitableView Search Text Sigbart Error - ios

117 Views Asked by At

I've implemented a search bar (and display controller) in a uitableview using the code outlined below. When I type in the search text box I get the following sigabrt error:

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

I've re-reviewed the tutorial I've been following and quadruple checked the code however I cant find the cause of the error, can anyone help?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [self.searchResults count];
    } else {
        return _restaurantsInCity.count;
    }
}

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

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    RestarauntEntity *currentRestaurant = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        currentRestaurant = [[self.searchResults objectAtIndex:indexPath.row]];
    } else {
        currentRestaurant = [self.restaurantsInCity objectAtIndex:indexPath.row];
    }

    NSString *decodedText1 = [currentRestaurant.title stringByReplacingOccurrencesOfString:@"’" withString:@"'"];
    NSString *decodedText = [decodedText1 stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
    cell.textLabel.text = decodedText;
    cell.textLabel.font = [UIFont fontWithName:@"avenir" size:16.0f];
    cell.textLabel.textColor=[UIColor blackColor];
    cell.detailTextLabel.text = currentRestaurant.city;
    cell.detailTextLabel.font = [UIFont fontWithName:@"avenir" size:12.0f];
    cell.detailTextLabel.textColor=[UIColor darkGrayColor];

    return cell;
}

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

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

    return YES;
}
2

There are 2 best solutions below

1
On

If you are not able to debug your current code, you can implement search bar functionality with help of UITextField too what I have done. Just put a UITextField instead of search bar, above your UITableView and set delegate. Make sure you are using correct name of variables of your data object class. I am writing my code here, let me know if this helps

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

if (isFromSearchResult) // BOOL to determine is from search result or not
{
    return [searchResults count];
}
else
{
    return myAppDelegate.offersArr.count;
}

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

customCell = (offersCustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"Cell"];

if(isFromSearchResult)
{
    dataObj = [searchResults objectAtIndex:indexPath.row]; //dataObj is object of NSObject data class
} else
{
    dataObj = [myAppDelegate.offersArr objectAtIndex:indexPath.row];
}

customCell.LblOffer.text = [dataObj.OfferTitle uppercaseString];

NSString *validity = @"VALID TILL: "; // dataObj.ValidTime;
customCell.LblValid.text = [validity stringByAppendingString:dataObj.ValidTime];

return customCell;

}

-(void)filterContentForSearchText: (NSString*)searchText scope:(NSString*)scope { //here searching for three fields- title, content and address

NSPredicate *predicateOfferTitle = [NSPredicate predicateWithFormat:@"OfferTitle contains[cd] %@", searchText];
NSPredicate *predicateOfferContent = [NSPredicate predicateWithFormat:@"OfferContent contains[cd] %@", searchText];
NSPredicate *predicateAddress = [NSPredicate predicateWithFormat:@"Address contains[cd] %@", searchText];

NSArray *subPredicates = [NSArray arrayWithObjects:predicateOfferTitle, predicateOfferContent,predicateAddress, nil];
NSPredicate *orPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];
searchResults = [myAppDelegate.offersArr filteredArrayUsingPredicate:orPredicate];
//NSLog(@"%@", searchResults);

}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

NSString *value = [textField.text stringByReplacingCharactersInRange:range withString:string];
//NSLog(@"value: %@", value);
if (![value isEqualToString:@""]) {
    [self filterContentForSearchText:value scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

    isFromSearchResult=YES;
}
else
{
    isFromSearchResult=NO;
}

[offersTable reloadData];
return true;

}

2
On

Try this

It happens if you have created any outlet or action on button that does't exists any more. Check all your actions and outlets. Go to your storyboard and right click on yellow sign of your viewcontroller and delete all actions and outlets with yellow sign.

enter image description here

Hope it helps.

libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

Try this answer hope it helps.