0

I'm getting the following error when i start type the search text in SearchBar

Assertion failure in -[UISearchResultsTableView _configureCellForDisplay:forIndexPath:]

My cellForRowAtIndexPath code for Search Display Controller is as follows :

if(tableView == self.searchDisplayController.searchResultsTableView)
{
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [search objectAtIndex:indexPath.row];

    return cell;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Vaibhav Jhaveri
  • 1,579
  • 3
  • 28
  • 53

1 Answers1

5

Replace your code with this one may be it will work.

if(tableView == self.searchDisplayController.searchResultsTableView)
{
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    cell.textLabel.text = [search objectAtIndex:indexPath.row];

    return cell;
}

because once you find that the cell object is nil again you are allocating the cell with nil object using [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; that's why it is not working.

iHulk
  • 4,869
  • 2
  • 30
  • 39
  • What if I want to use custom Cell? – Vaibhav Jhaveri Nov 01 '14 at 06:25
  • I dont have a Custom XIB file differently – Vaibhav Jhaveri Nov 01 '14 at 06:30
  • you can also follow the tutorial on these links http://www.appcoda.com/customize-table-view-cells-for-uitableview/ or http://www.icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/ it will explain you the full process. – iHulk Nov 01 '14 at 06:30
  • if you are using storyboard you do not need to create a separate xib for your cell but can crete it inside the table only and also do not need to register it programatically. – iHulk Nov 01 '14 at 06:33
  • now if you have created the nib inside the table view then just give it a CellIdentifier and use it inside the cellforrowatindexpath as CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; also create a CustomCell as subclass of UITableViewCell and set your prototype cell parent class as Your CustomClass – iHulk Nov 01 '14 at 06:42
  • You can also find a good tutorial regarding use of custom cell using storyboard on http://www.appcoda.com/ios-programming-customize-uitableview-storyboard/ – iHulk Nov 01 '14 at 06:48