Doug Smith
Doug Smith

Reputation: 29314

Why does the label never show up in my UITableViewCell subclass?

Example project: http://cl.ly/283O3a0x2l3h

I'm trying to create a custom cell with some views in it, so I subclassed UITableViewCell as shown below. That subclass has a UILabel that gets created on init and positioned in updateConstraints.

However, whenever I run the app, it never shows the label on the cells. In fact, the init method for the UITableViewCell subclass is never end called.

This is called in cellForRowAtIndexPath:

cell.postTitle.text = @"testing";

However initWithStyle: in the UITableViewCell subclass is never once called, and I can't figure out why. It's worth noting that it has a bit done from Interface Builder, but just the UITableView set up with the cell deemed to be a subclass of UITableViewCell.

Can anyone take a look and tell me what I did wrong?

Upvotes: 0

Views: 217

Answers (3)

Rob
Rob

Reputation: 437552

If you're going to do it programmatically, use initWithCoder. Even easier, create IBOutlet references in you cell prototype, and you can eliminate all of that code which is creating it programmatically altogether. Just define your outlets and constraints in code, and then you can retire all of that code inside the CSPostCell implementation:

outlets for table view cell

So you enjoy the benefits of the CSPostCell subclass, but without any of the code.

Upvotes: 1

jlehr
jlehr

Reputation: 15597

When the instance of your custom UITableViewCell subclass is loaded at runtime, its initWithCoder: method is called instead of the initWithStyle: method. (You would call initWithStyle: if you created a new instance with alloc, but that's not the case here.) To fix the problem, simply change the method signature accordingly.

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self == nil) return nil;

    // Initialization code goes here...

    return self;
}

For more insight into how objects are deserialized from nib files and storyboards, see the Archives and Serializations Programming Guide

Upvotes: 0

Duncan Groenewald
Duncan Groenewald

Reputation: 8988

Here is an example of how to load up a custom cell from a NIB file. The custom tableView cell (FileTableViewCell) is created in IB and contains two labels, a progress bar, a button and sometimes an activity indicator gets added to the accessoryView. So try just replacing the cell = [nib... line with cell = [YourSubClass...] and then modify the rest as required by your app.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"FileTableViewCell";
    //LOG(@" tableView:cellForRowAtIndexPath:");
    FileTableViewCell *cell = (FileTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        //LOG(@" cell is nil so create one");
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FileTableViewCell" owner:self options:nil];
        //FLOG(@" nib is %@", nib);
        cell = [nib objectAtIndex:0];
    }

    // Configure the cell.
    FileRepresentation* fileRepresentation = _fileList[indexPath.row];
    cell.textLabel.text = [self userFilename:[fileRepresentation.fileName stringByDeletingPathExtension]];

    cell.detailTextLabel.text = [fileRepresentation modifiedDate];

    float percentage = [fileRepresentation.percentDownloaded intValue] / 100.0;
    int ds = [self downloadStatus:fileRepresentation];


    if (ds == 1) {
        //FLOG(@" download process for file is %f", percentage);
        cell.textLabel.textColor = [UIColor grayColor];
        cell.accessoryType = UITableViewCellAccessoryNone;
        UIActivityIndicatorView *progressView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        cell.accessoryView = progressView;
        [cell.progressIndicator setHidden:NO];
        [cell.progressIndicator setProgress:percentage];
        [progressView startAnimating];
    }
    if (ds == 2) {
        //FLOG(@" download process for file is %f", percentage);
        cell.textLabel.textColor = [UIColor grayColor];
        cell.accessoryType = UITableViewCellAccessoryNone;
        UIActivityIndicatorView *progressView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        cell.accessoryView = progressView;
        [cell.progressIndicator setHidden:NO];
        [cell.progressIndicator setProgress:percentage];
        [progressView startAnimating];
    }
    if (ds == 3) {
        cell.textLabel.textColor = [UIColor blackColor];
        cell.accessoryView=nil;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [cell.progressIndicator setHidden:YES];

    }
    cell.imageView.image = [UIImage imageNamed:_fileImageName];
    return cell;
}

Upvotes: 0

Related Questions