UITableViewCell没有显示UILabel

时间:2010-11-30 20:45:22

标签: iphone objective-c uikit uitableview uilabel

我有这个代码,它给出了下面截图的结果。它位于tableView:cellForRowAtIndexPath:方法中。我不知道为什么栏上方的文字没有显示。有没有人有任何想法?谢谢。

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];

alt text

2 个答案:

答案 0 :(得分:4)

只是在黑暗中刺伤,但因为你做cell.textLabel.text = @"";,它可能会将textLabel的框架缩小到宽度为0.尝试根据你知道的框架创建标签正确的可见尺寸。

答案 1 :(得分:1)

要显示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.
    }

注意:如果您使用的tableViewCell高于默认大小44.0,则可以在标签的initWithFrame方法中更改25号。
希望这可以帮助你:)