3

enter image description here

#pragma Search Methods

-(void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@",searchText];
    _AramaSonuclari = [_TarifAdi filteredArrayUsingPredicate:predicate];
    
}

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath];
    
    cell.tag = indexPath.row;
    //cell.imageView.image = nil;
    
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        cell.TitleLabel.text = _AramaSonuclari[indexPath.row];
    }
    else
    {
        cell.TitleLabel.text = _TarifAdi[indexPath.row];
    }

    return cell;
 }

Our problem, when we try to enter any character in the search bar, app crashes, we did debug our code work without error. We think, our problem is on storyboard connection; also I added an image. When we delete searchBar referencing outlets, we can type something but of course the code is not working without connection.

Error Log:

2014-07-14 13:29:08.577 SevgiLezzeti[3839:60b] *** Assertion failure in -[UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2935.137/UITableView.m:5439

2014-07-14 13:29:08.582 SevgiLezzeti[3839:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier TableViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

Community
  • 1
  • 1
Gökhan Çokkeçeci
  • 1,258
  • 3
  • 15
  • 35

2 Answers2

4

You have to register the cell to searchdisplaycontroller as well.

Important: You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.

in View did load

If you are Creating cell in code

 [self.searchDisplayController.searchResultsTableView registerClass:[TableViewCell class] 
    forCellReuseIdentifier:@"IdentifierForCell"];

If you are Creating cell in nib

  [self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"CellNibName" bundle:nil]
                                        forCellWithReuseIdentifier:@"IdentifierForCell"];
1

I solved it like that :

at this line :

TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath];

you need to delete "forIndexPath:indexPath" so:

 TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell"];
Almog_0
  • 413
  • 4
  • 10
  • This solved the problem in the op but then I got "fatal error: unexpectedly found nil while unwrapping an Optional value" – Danny Wang Mar 18 '16 at 03:40
  • 1
    You are using in swift, u should check if one of the fields isn't nil. http://stackoverflow.com/questions/24643522/fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-values – Almog_0 Mar 19 '16 at 15:11