newguy222
newguy222

Reputation: 131

Programmatically creating custom cells in objective c?

I'm having issues getting customs cells to show up. Below I've posted a simplified version of my code:

 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    NSLog(@"Being Called?");
    //reuseID = @"newtableid";
    self.backgroundColor = [UIColor redColor];

    self.name = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 50)];
    self.name.backgroundColor = [UIColor redColor];

    [self addSubview:self.name];

    return self;
}

In my viewController, I set up the tableView in the ViewDidLoad method:

 self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 400) style:UITableViewStylePlain];

 self.table.delegate = self;
 self.table.dataSource = self;
 self.table.backgroundColor = [UIColor blackColor];

 [self.view addSubview:self.table];
 [self.table registerClass:NewTableViewCell.class forCellReuseIdentifier:@"Cell"];

Then, I complete the cellForRowAt method like so:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *simpleTableIdentifier = @"Cell";

    NewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[NewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

    cell.backgroundColor = [UIColor blackColor];

    cell.name.text = [_familyDetails objectAtIndex:indexPath.row];

    return cell;
}

The problem is, I don't get anything to show up on my cells. My NewTableVIewCell constructor is called, but the cells show up blank (without color, label, etc.) What am I doing wrong?

Upvotes: 2

Views: 3688

Answers (2)

Simon
Simon

Reputation: 6523

If you're creating the cell programatically, make sure you register your cell to your table view.

[self.tableView registerClass:UITableViewCell.class forCellReuseIdentifier:"yourCellId"];

I am using a generic UITableViewCell in this case. You can try to change the text label of it in cellForRowAtIndexPath to see whether that works.

Be sure to change it to your custom cell class if needed.

Upvotes: 5

Adam
Adam

Reputation: 4828

In order to dequeueReusableCellWithIdentifier, you have to use

- (void)registerClass:(Class)cellClass 
forCellReuseIdentifier:(NSString *)identifier

and normally you will not even have to instantiate NewTableViewCell if you use dequeueReusableCellWithIdentifier:forIndexPath:. However, if you still use dequeueReusableCellWithIdentifier: then you still need to instantiate your cell subclass (thanks for reminding me Alejandro Ivan).

EDIT: So you have to write something like this, in your viewDidLoad for example :

- (void)viewDidLoad {
    [super viewDidLoad];

    // Your stuff...
    [self.table registerClass:[NewTableViewCell class] forCellReuseIdentifier: simpleTableIdentifier];
}

Upvotes: 1

Related Questions