Skip to content

iOS: Load custom UITableViewCell with Nib

Using the interface builder i created a custom 'UITableViewCell' named 'MyCustomCellNib', with a custom class file 'MyCustomCell' inherited from 'UITableViewCell'. This is code i used to load the cell with the nib.

Using the interface builder i created a custom ‘UITableViewCell’ named ‘MyCustomCellNib’, with a
custom class file ‘MyCustomCell’ inherited from ‘UITableViewCell’.

This is code i used to load the cell with the nib.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCustomCell *cell = (MyCustomCell *) [tableView dequeueReusableCellWithIdentifier:@"MyCustomCellIdentifier"];
 
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCellNib" owner:self options:nil];
        cell = [topLevelObjects objectAtIndex:0];
 
	// cell is a 'MyCustomCell' object.
    }
 
    return cell;
}

Hope it helps! ๐Ÿ™‚

Dipin Krishna

Written by Dipin Krishna

Senior full-stack engineer โ€” 15 years across Django, Laravel, SwiftUI and the infrastructure underneath. Available for contract work.

Work with me โ†’

Leave a note

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.