Xib file of UITableViewCell

suggest change

Create a UITableView cell category class.

UITableViewCell+RRCell.h file

#import <UIKit/UIKit.h>

@interface UITableViewCell (RRCell)

-(id)initWithOwner:(id)owner;

@end

UITableViewCell+RRCell.m file

#import "UITableViewCell+RRCell.h"

@implementation UITableViewCell (RRCell)

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-designated-initializers"

-(id)initWithOwner:(id)owner {
    
    if (self = [super init]) {
        
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:owner options:nil];
        self = [nib objectAtIndex:0];
    }
    return self;
}

#pragma clang diagnostic pop



@end

initWithOwner: still replaces self with the first object in the nib, so the instance alloc created is discarded. An empty nib crashes at objectAtIndex:0. Import the cell category and call it from cellForRowAtIndexPath. The method below creates a new cell on every call and does not reuse one.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Created a custom cell XIB and loaded it through the cell category
    CustomCell *cell = [[CustomCell alloc] initWithOwner:self];
    
    return cell;     
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents