2010-11-30 92 views
2

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

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

回答

4

在黑暗中只是一个尝试,而是因为你没有cell.textLabel.text = @"";,它可能已经缩小了为textLabel的框架有基于断帧的0尝试创建标签的宽度你知道这是一个正确的可见尺寸。

+0

好的,谢谢。它确实在其他地方使用该框架工作。我会尽力改变它 – 2010-11-30 20:56:00

+0

干杯。这很有用。 – 2010-11-30 21:05:18

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号标签。
希望这可以帮助你:)