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.

- (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! 🙂

Leave a comment

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.