nerraga
nerraga

Reputation: 614

UITableViewCell detailTextLabel never displays

UITableViewCell detailTextLabel never displays.

No xib, no storyboard. I've tried UITableViewCellStyleValue1, UITableViewCellStyleValue2, and UITableViewCellStyleSubtitle. I'm not using subclassed UITableViewCells. I've tried both detailTextLabel.text and detailTextLabel.attributedText. I've verified that the re-use ID isn't used anywhere else and I've stepped through and confirmed that it's the correct value. The textlabel works and so does the accessoryType if I set it. It's just the detailTextLabel that refuses to display.

Notably, the (cell == nil) condition is never hit and I have no idea why. I've tried setting the re-use ID to gibberish just to see if it'd have any effect with no success. What have I missed?

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class]
           forCellReuseIdentifier:@"reuseCellID"];
    [self setTitle:@"Title"];
}


- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *reuseCellID = @"reuseCellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseCellID
                                                            forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1
                                     reuseIdentifier:reuseCellID];
        cell.detailTextLabel.text = @"detail (one)";
    }

    cell.textLabel.text = self.collection[indexPath.row];
    cell.detailTextLabel.text = @"detail (two)";
    /*NSMutableAttributedString *textLabelStr =
        [[NSMutableAttributedString alloc] initWithString:@"attributed detail"];
    cell.detailTextLabel.attributedText = textLabelStr;*/

    return cell;
}

Upvotes: 0

Views: 243

Answers (1)

rmaddy
rmaddy

Reputation: 318814

if (cell == nil) is never hit because you use dequeueReusableCellWithIdentifier:forIndexPath: and registerClass: forCellReuseIdentifier:.

Get rid of the call to registerClass: forCellReuseIdentifier: and use dequeueReusableCellWithIdentifier: (without the forIndexPath:.

Then your code will do what you expect.

BTW - don't set the detailTextLabel.text inside if (cell == nil) unless you want every cell to have the same detail text.

Upvotes: 2

Related Questions