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.)
After trying all of these things, when I set the table view cell style to "Subtitle" in Storyboard, the subtitle showed up for me
Registering a standard UITableViewCell
class might prevent you using styled UITableViewCell
s because this code will never be executed:
if (cell == nil) {
// Cell will never be nil if a class/nib is registered
}
Solution: Are you registering a nib or Class
to your UITableView
? If you have a line like the following, try deleting it:
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:"cellReuseID"];
and updating your dequeue statement, like follows:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:"cellReuseID"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:"cellReuseID"];
}
OK, it turns out that we've got a very unusual build process on this app, which was not correctly identifying the SDK the app was built against to the Cocoa framework.
As a result, Cocoa thought this was a very old app -- from the pre-iOS4 days, before detailTextLabel was added. So, the framework was trying to be helpful by emulating the pre-iOS4 behavior (by hiding detailTextLabel in several ways). Setting a breakpoint on _UIApplicationLinkedOnOrAfter, and forcing it to return 1, allowed me to hack around this feature and prove that it was the cause. (And now we just need to fix our build process to report the correct SDK version.)
Needless to say, this isn't something most people are going to run into. But I thought I'd post the answer here for posterity's sake anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With