Xib file of UITableViewCell
suggest changeCreate 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;
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents