Search code examples
iosuitableviewstoryboarduisearchbaruisearchdisplaycontroller

How to populate custom cell while using UISearchBar in storyboard


I'm using storyboard for a tableview with custom cells. I created custom cells in storyboard without any additional classes for it. Everything works fine when I'm not using the UISearch bar, But when I want to create the custom cell for UISearchBarController tableview I don't know how can I do it. Hers is my custom cell in storyboard,

custom cell

with cellIdentifier = "SelectionCell"

cell identifier

and I'm not using any custom class.

custom class

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SelectionCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)  {
    NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
    cell = [nibs objectAtIndex:0];
}

the code crashes: 'Could not load NIB in bundle:'

I don't have a NIB file for my custom cell, and I don't have any custom UITableViewCell class to use alloc/init method like this:

cell = [[myCustomCellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

how can I create the cell?


Solution

  • It seems it can't be done without a custom cell class. Because we don’t have a storyboard prototype cell to manipulate graphically for the filtered table view, we have to programmatically create the cells. And for that we need a custom cell class and with initWithStyle: method in the .m file as follows. So I created a class called SelectionCell subclass of UITableViewCell, and in IB assigned custom cell class to SelectionCell class and create IBOutlet for all UI controls in my custom cell into SelectionCell.h file

    @interface SelectionCell : UITableViewCell{
    
    }
    @property (strong, nonatomic) IBOutlet UIImageView *contactPictureID;
    @property (strong, nonatomic) IBOutlet UILabel *contactName;
    @property (strong, nonatomic) IBOutlet UILabel *emailAddress;
    @property (strong, nonatomic) IBOutlet UIImageView *selectedEmailState;
    
    @end
    

    in initWithStyle: method, I had to recreate all the UI controls programmatically and add them to the contentView as subviews:

    // SelectionCell.m
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            // Initialization code
            self.selectionStyle = UITableViewCellSelectionStyleNone;
    
            _contactPictureID = [[UIImageView alloc] initWithFrame:(CGRectMake(10, 14, 48, 48))];
    
            _contactName = [[UILabel alloc] initWithFrame:(CGRectMake(66, 14, 201, 30))];
            _contactName.font = [UIFont boldSystemFontOfSize:20];
            _contactName.textAlignment = NSTextAlignmentLeft;
    
            _emailAddress = [[UILabel alloc] initWithFrame:(CGRectMake(66, 41, 201, 21))];
            _emailAddress.font = [UIFont systemFontOfSize:16];
            _emailAddress.textAlignment = NSTextAlignmentLeft;
    
            _selectedEmailState = [[UIImageView alloc] initWithFrame:(CGRectMake(275, 22, 32, 32))];
    
            [self.contentView addSubview:_contactPictureID];
            [self.contentView addSubview:_contactName];
            [self.contentView addSubview:_emailAddress];
            [self.contentView addSubview:_selectedEmailState];
        }
        return self;
    }
    

    Now I am able to instantiate my custom cell. In cellForRowAtIndexPath method

    static NSString *CellIdentifier = @"SelectionCell";
    SelectionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[SelectionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; }
    

    And now everything is working as it should be.