简体   繁体   English

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. 它在tableView:cellForRowAtIndexPath:方法中。 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];

替代文字

Just a stab in the dark, but because you did cell.textLabel.text = @""; 只是在黑暗中刺伤,但是因为您做了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. ,它可能会将textLabel的框架缩小到了0的宽度。请尝试根据已知尺寸正确的框架创建标签。

To show your 2 labels, you can use this code: 要显示2个标签,可以使用以下代码:

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

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. 注意:如果使用的tableViewCell大于默认大小44.0,则可以在标签的initWithFrame方法中更改25号。
Hope this can help you :) 希望这可以帮到你 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM