Why is my UITableViewCell not showing detailTextLabel in ANY style?

只愿长相守 提交于 2019-12-04 23:41:19

问题


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.)


回答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




回答2:


Registering a standard UITableViewCell class might prevent you using styled UITableViewCells 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"];
}



回答3:


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.




回答4:


i hope this will helps you ,once check it

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    } 
    cell.textLabel.text = @"Foo";
    cell.detailTextLabel.text = @"Bar";
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.frame = CGRectMake(10, 20, 100,22);        
        return cell;
}

the operations like font size ,background color changing for cell.labels these all the functionalities you have to do in this below method.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:10];  
    cell.textLabel.backgroundColor=[UIColor redColor];
    cell.textLabel.frame = CGRectMake(10, 20, 100,22); 
    cell.detailTextLabel.frame = CGRectMake(10, 20, 200,22); 

}

if you are changing these type of properties in cellForRowAtIndexPath this method then you will lost those properties.




回答5:


The problem is you most likely registered your cell using

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

Meaning

if (not cell) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:reuseID] autorelease];
}

will never be called because the cell will never be null. Therefore the cell will always be initialized to UITableViewCellStyleNone by the table.

The only way to fix this is to register the cell from the storyboard or to subclass the UITableViewCell and override the

(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

method.

This works for me:

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

    }
    return self;
}


来源:https://stackoverflow.com/questions/15424453/why-is-my-uitableviewcell-not-showing-detailtextlabel-in-any-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!