RESOLVED: see bottom of question.
I am using a custom UITableViewCell for my programmatically defined UITableView. The custom UITableViewCell currently has a red background and nothing else. Here is the snippet from the UIViewController’s cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_ID];
if (cell == nil)
{
cell = [self getNewCell];
}
…
return cell;
}
- (CCTableViewCell *) getNewCell
{
return [CCTableViewCell newCell];
}
Related code in CCTableViewCell:
in the .h:
#define CELL_ID @"CCTVCell"
in the .m:
+ (CCTableViewCell *) newCell
{
CCTableViewCell * cell = [[CCTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL_ID];
return cell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self initCell];
}
return self;
}
- (CCTableViewCell *) init
{
self = [super init];
if (self) {
[self initCell];
}
return self;
}
- (void) initCell
{
[self setBackgroundColor:[UIColor redColor]];
}
The issue is that the custom UITableViewCell is not being used (either not displaying or not being created) in the table. Instead, a standard cell is being used.
I looked at a lot of other questions and all are solved by steps I've already implemented.
Note: The separation of parts in this is since this will be a parent class to specific UITableViewControllers with their own implementations of CCTableViewCell. This part has the common code for inheritance.
EDIT: CCTableViewCell code added
EDIT++:
This is in my UIViewController's viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(50, 100, 220, 300)];
tableView.delegate = self;
tableView.dataSource = self;
tableView.allowsMultipleSelection = NO;
[tableView registerClass:[CCTableViewCell class] forCellReuseIdentifier:CELL_ID];
[tableView reloadData];
[self.view addSubview:tableView];
}
EDIT++: The TableViewController is really UIViewController with the delegate and datasource set up
EDIT: RESOLVED: So it turns out that overwriting the willDisplayCell delegate works. See the link below for information.
How to customize the background color of a UITableViewCell?
Essentially, cellForRowAtIndexPath seems to only do things post-creation of the table. The willDisplayCell works whenever the cell is displayed. My guess is that the project created blank cells when creating the table in viewDidLoad.