2010-11-30 59 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號標籤。
希望這可以幫助你:)