1

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.

Community
  • 1
  • 1
DrDisc
  • 281
  • 4
  • 12
  • Add the code for `CCTableViewCell` – Wain Jun 18 '13 at 20:29
  • try replacing this line: CCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_ID]; by: CCTableViewCell *cell = (CCTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CELL_ID]; I am not sure if thats the reason... wont lose anything if you try.. good luck – DrDev Jun 18 '13 at 20:31
  • One more thing, where do you load your custom cell NIB ? – DrDev Jun 18 '13 at 20:33
  • I would do it this way :: if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CELL_ID owner:self options:nil]; cell = [nib objectAtIndex:0]; } – DrDev Jun 18 '13 at 20:34
  • I do not have a pre-existing nib. This is why I put the title as "programmatic UITableViewCell" @DrDev thanks for the attempt. It didn't do anything new. – DrDisc Jun 18 '13 at 20:37
  • Yeah I just realized... Will let you know if i have any other ideas.. – DrDev Jun 18 '13 at 20:39

2 Answers2

0

Are you running registerClass:forCellReuseIdentifier: or registerNib:forCellReuseIdentifier: after your tableView has been created? This is what tells your table view to use your custom class/nib when creating a cell. You would likely do this in the viewDidLoad method of your view controller.

As of iOS 6 dequeueReusableCellWithIdentifier: always returns a cell if you have registered a class using one of the register???:forCellReuseIdentifier: methods. Previously it would return nil if a free cell was not available. You don't need to test cell == nil if you are building for iOS 6.

EDIT: You mention this is a TableViewController. I assume this means your view controller is a subclass of UITableViewController. If this is the case there is already an instance if UITableView available via the tableView property and no need to create one in your viewDidLoad method. Instead I would change your code to look like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.allowsMultipleSelection = NO;
    [[self tableView] registerClass:[CCTableViewCell class] forCellReuseIdentifier:CELL_ID];
    [[self tableView] reloadData];
}

In fact, you might not have to set the delegate and dataSource properties if you are using a UITableViewController (I'm not sure if this is already done for you). Have you tried adding an NSLog in tableView:cellForRowAtIndexPath: to be sure it's being called?

Alex
  • 1,625
  • 12
  • 18
  • @Alex Your first paragraph isn't true if you don't use one of the "registerXXX:forCellReuseIdentifier:" methods. – rmaddy Jun 18 '13 at 20:37
  • @rmaddy Thanks for the note. I'll edit my comment to reflect this information. – Alex Jun 18 '13 at 20:40
  • The TableViewController is actually a TableView with the delegate and datasource set up. Sorry for the confusion. I will update my post. – DrDisc Jun 18 '13 at 20:59
  • Thank you for the attempts. It turned out it was a different path. I'd give you vote-up for answering, but I just joined and don't have enough rep yet. – DrDisc Jun 18 '13 at 21:09
0

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.

Community
  • 1
  • 1
DrDisc
  • 281
  • 4
  • 12