pouncelciot
pouncelciot

Reputation: 51

UISearchBar without a common tableViewCell

I am following this example to set up a UISearchController in my project: https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html#//apple_ref/doc/uid/TP40014683-Intro-DontLinkElementID_2

In the example they programmatically set up one tableViewCell to use for both the main table view and the search result, while I have got the cells that I will be using already, in both the main's and the search result's tableviews.

If I use the Storyboard ID's in mainTableView and searchResultsTableView's cellForRowAtIndexPath, there is a crash: 'unable to dequeue a cell with identifier SearchResultCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'.

Any help with this is very appreciated!

Upvotes: 0

Views: 184

Answers (1)

0yeoj
0yeoj

Reputation: 4550

What i mean is create a .xib like this:

enter image description here

Configure the UI of your cell and Set identifier like: tableID

enter image description here

Set custom class dTableViewCell for the property (IBOutlet)

enter image description here

Then declare a property for the class:

@property (nonatomic) dTableViewCell *tableCell;
@property (nonatomic) dSearchTableViewCell *tableCell;

Then inside your UITableViewDelegate :

- (UITableViewCell *)tableView:tableView cellForRowAtIndexPath:indexPath
{

    if (tableView == self.mainTableView)
    {
        static NSString *cellID = @"tableID";

        //dTableViewCell.xib for mainTableView ]> example...
        [tableView registerNib:[UINib nibWithNibName:@"dTableViewCell" bundle:nil] forCellReuseIdentifier:cellID];

        self.tableCell = [tableView dequeueReusableCellWithIdentifier:cellID];
    }
    else //searchTableView
    {
        static NSString *cellSearchID = @"searchTableID";

        //dSearchTableViewCell.xib for searchTable ]> example...
        [tableView registerNib:[UINib nibWithNibName:@"dSearchTableViewCell" bundle:nil] forCellReuseIdentifier:cellSearchID];

        self.searchTableCell = [tableView dequeueReusableCellWithIdentifier:cellID];
    }

}

also check your SearchResultCell identifier in your storyboard, because the crash is telling you that SearchResultCell is not registered.. Hope i've help you.. Happy coding cheers..

Upvotes: 1

Related Questions