UITableViewCell 未在其中顯示 UILabel (UITableViewCell not showing UILabel within)


問題描述

UITableViewCell 未在其中顯示 UILabel (UITableViewCell not showing UILabel within)

I have this code and it gives the result of the screenshot below. It's in the tableView:cellForRowAtIndexPath: method. I don't know why the text above the bars isn't showing. Does anyone have any ideas? Thank you.

cell.textLabel.text = @"";
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.textAlignment = UITextAlignmentLeft;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UILabel * label = [[UILabel alloc] initWithFrame: cell.textLabel.frame];
    label.backgroundColor = [UIColor clearColor];
    label.text = [NSString stringWithFormat:@"%.1f%% (%i)",([[options_votes objectAtIndex: [indexPath row]] intValue] * 100.0 / total_votes),[[options_votes objectAtIndex: [indexPath row]] intValue]];
    NSLog(@"%@",[NSString stringWithFormat:@"%.1f%% (%i)",([[options_votes objectAtIndex: [indexPath row]] intValue] * 100.0 / total_votes),[[options_votes objectAtIndex: [indexPath row]] intValue]]);
    label.textAlignment = UITextAlignmentRight;
    [cell.contentView addSubview:label];
    [label release];
    label = [[UILabel alloc] initWithFrame: cell.textLabel.frame];
    label.backgroundColor = [UIColor clearColor];
    label.text = [options objectAtIndex:[indexPath row]];
    NSLog(@"%@",[options objectAtIndex:[indexPath row]]);
    label.textAlignment = UITextAlignmentLeft;
    [cell.contentView addSubview:label];
    [label release];
    UIImageView * image_view = [[UIImageView alloc] initWithImage:nav_delegate‑>back_bar];
    image_view.frame = CGRectMake(10, 44, 280, 10);
    [cell.contentView addSubview:image_view];
    [image_view release];
    image_view = [[UIImageView alloc] initWithImage:nav_delegate‑>blue_bar];
    image_view.frame = CGRectMake(10, 44, 5 + [[options_votes objectAtIndex: [indexPath row]] intValue] * 275.0/total_votes, 10);
    [cell.contentView addSubview:image_view];
    [image_view release];

‑‑‑‑‑

參考解法

方法 1:

Just a stab in the dark, but because you did cell.textLabel.text = @"";, it might have shrank the frame of the textLabel there to a width of 0. Try creating your labels based off of a frame that you know to be of a correct visible size.

方法 2:

To show your 2 labels, you can use this code:  

cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (indexPath.row == 0) {
        UILabel label1 = [[UILabel alloc] initWithFrame:CGRectMake(11, 10, 280, 25)];
        label1.backgroundColor = [UIColor clearColor];
        label1.text = @"Your Text Here";
        label1.textAlignment = UITextAlignmentRight;
        [cell.contentView addSubview:label1];
        [label1 release];
    }
    if (indexPath.row == 1) {
        UILabel 
label2 = [[UILabel alloc] initWithFrame:CGRectMake(11, 10, 280, 25)];
        label2.backgroundColor = [UIColor clearColor];
        label2.text = @"Your Text Here";
        label2.textAlignment = UITextAlignmentLeft;
        [cell.contentView addSubview:label2];
        [label2 release];
    }
    if (indexPath.row == 2) {
        // Put your imageView code here.
    }</code></pre>
  

Note: if you are using a tableViewCell taller than defaults size, 44.0, you can change the 25 number in the initWithFrame method of the labels. Hope this can help you :)

(by Matthew MitchellDavid Liumatteodv)

參考文件

  1. UITableViewCell not showing UILabel within (CC BY‑SA 3.0/4.0)

#uilabel #objective-c #iphone #uitableview #uikit






相關問題

標籤邊框內的間距 (spacing inside label's border)

awakeFromNib 無法從 UILabel 獲取值,也無法禁用 UIButton (awakeFromNib can't get value from UILabel and can't disable UIButton)

如何在 UILabel 中製作陰影 (How to make shadow in UILabel)

如何計算 UILabel 的字體大小 (How can I calculate font size of UILabel)

如何在情節提要中為uilabel設置大於300的字體大小? (How to set font size greater than 300 for uilabel in storyboard?)

隨著 UILabel 尺寸的增加,在我的 UILabel 周圍獲得額外的空間 (Getting extra space around my UILabel as it increases in size)

UILabel 不顯示所有內容 (UILabel not displaying everything)

在 UIView 中將標籤居中 (Centering a label in a UIView)

UITableViewCell 未在其中顯示 UILabel (UITableViewCell not showing UILabel within)

帶有 UIMenucontroller 的 UILabel 不會通過外部觸摸退出第一響應者 (UILabel with UIMenucontroller not resigning first responder with touch outside)

UILabel 動態高度問題 (UILabel Dynamic height problem)

是否可以將標籤內的文本與“大”框架垂直對齊 (Is it possible to vertically align text inside labels with a "large" frame)







留言討論