0
votes

I tried to implement a SearchBarView Controller to my TableView with CoreData. But, when I tap on the Searchbar, the app crashes and I get the following Error:

Assertion failure in -[UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-2935.137/UITableView.m:5439
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

Without Searchbar I show my TableContent like this:

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

    // Configure the cell...
    NSManagedObject *device = [self.inventory objectAtIndex:indexPath.row];
    [cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"productName"]]];
    //[cell.detailTextLabel setText:[device valueForKey:@"company"]];

    return cell;
}

works perfectly.

So now I tried to show the SearchbarViewControllerContent like this:

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

    // Configure the cell...
    NSManagedObject *device = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView)
        {device = [searchResults objectAtIndex:indexPath.row];}
    else
        {device = [self.cart objectAtIndex:indexPath.row]; }

    [cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"productName"]]];

    return cell;
}

but this doesn't work.

If you have to know how my Searchbar (should) works:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 71;
}

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

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

    return YES;
}
1
same problem, but different approachesuser255368
Has Anyone another Solution?user255368
What do you mean by "another solution"? Which solutions did you try from the answers to the duplicate question? Why did they not work?Martin R
I'm sorry... Okay so i worked trough the duplicate Topic and solved the Problem with the crashing App. But when i type a letter in the search bar returns "No Results". How can i be sure, that the search bar is searching in my CoreData? Or has this "No Results"-Error nothing to do with it?user255368

1 Answers

0
votes

You have to register the cell for search tableView in viewDidLoad method such as:

  [self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:<Nib Name> bundle:nil] forCellReuseIdentifier:<cellIdentifier>]