Ios – Why is the UITableViewCell not showing detailTextLabel in ANY style

detailtextlabeliosuitableview

This one has me pulling my hair out. I'm just trying to create a table with the UITableViewCellStyleSubtitle style, with text and detail. But the detail isn't showing up. Usually this is because someone has forgotten to initialize the cells with the correct style, but that's not what's going on in this case. I've tried all four cell styles, and the detail doesn't show up in any of them, though the text label does move over to the right when I use UITableViewCellStyleValue2.

I've successfully created tables many times before. The only significant difference I can think of in this case is that I'm not using a UITableViewController. Instead, I'm embedding the UITableView like any other view, and hooking up my own data source and delegate.

I've boiled down the key method to this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Get the UITableViewCell (out of the recycling pool, or de novo)
    NSString *reuseID = @"CELL";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    if (not cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:reuseID] autorelease];
    }

    cell.textLabel.text = @"Foo";
    cell.detailTextLabel.text = @"Bar";
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.detailTextLabel.frame = CGRectMake(10, 20, 200,22);        // required
    cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:12];   // also required
    //[cell setNeedsLayout];  (makes no difference)
    return cell;
}

I can see the detail text only if I include BOTH of the "required" lines above (and use any cell style other than Default), and even when I do that, the position of the text label completely overlaps the position of the detail label.

So it appears as though the initializer is creating the detailTextLabel UILabel, but setting its font size to zero and its frame to something empty or offscreen. (I've tried inspecting these in the debugger, but it's not very useful — the font size is zero and the frame empty for BOTH the textLabel and detailTextLabel, yet the textLabel always shows up fine.)

Obviously I can work around it if I have to, by manually adjusting the label sizes and fonts. But it greatly disturbs me that I'm having to do that in this case, when normally you just set the style and text and layout is automatic. I've searched the googles and the stack overflows, but can't find any reference to a similar problem. Does anybody have any idea what's going on here?

(Testing under iOS 6.1.)

Best Answer

After trying all of these things, when I set the table view cell style to "Subtitle" in Storyboard, the subtitle showed up for me IB Storyboard Cell setting

Related Topic