3
votes

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'

2
what is crash log and how did you sure your code is currectNitin Gohel
did you define the cell id to be "TableViewCell" in IB ?giorashc
Yes i defined my code work without search when i try to searchBar still no problem but when i try to type something instant crashGökhan Çokkeçeci
try using self.tableView when dequeueing the cell instead of tableView since tableView may not be the instance of the one where the cell id has been definedgiorashc
This has nothing to do with Xcode.dasdom

2 Answers

4
votes

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
votes

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"];