Note that there are some explanatory texts on larger screens.

plurals
  1. POUITableView is not using programmatic custom UITableViewCell
    text
    copied!<p>RESOLVED: see bottom of question.</p> <p>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:</p> <pre><code>- (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]; } </code></pre> <p>Related code in CCTableViewCell:</p> <p>in the .h:</p> <pre><code>#define CELL_ID @"CCTVCell" </code></pre> <p>in the .m:</p> <pre><code>+ (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]]; } </code></pre> <p>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.</p> <p>I looked at a lot of other questions and all are solved by steps I've already implemented.</p> <p>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.</p> <p>EDIT: CCTableViewCell code added</p> <p>EDIT++:</p> <p>This is in my UIViewController's viewDidLoad</p> <pre><code>- (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]; } </code></pre> <p>EDIT++: The TableViewController is really UIViewController with the delegate and datasource set up</p> <p>EDIT: RESOLVED: So it turns out that overwriting the willDisplayCell delegate works. See the link below for information.</p> <p><a href="https://stackoverflow.com/questions/281515/how-to-customize-the-background-color-of-a-uitableviewcell">How to customize the background color of a UITableViewCell?</a></p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload