0

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;
}
user255368
  • 17
  • 6
  • possible duplicate of [Assertion failure when using UISearchDisplayController in UITableViewController](http://stackoverflow.com/questions/14207142/assertion-failure-when-using-uisearchdisplaycontroller-in-uitableviewcontroller) – Martin R Mar 14 '14 at 18:10
  • same problem, but different approaches – user255368 Mar 14 '14 at 18:37
  • Has Anyone another Solution? – user255368 Mar 15 '14 at 07:48
  • 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 Mar 15 '14 at 07:50
  • 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 Mar 16 '14 at 16:23
  • That's a different question, so better post a new one ... – Martin R Mar 16 '14 at 16:31

1 Answers1

0

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>]
Uma Madhavi
  • 4,851
  • 5
  • 38
  • 73