UITableViewCell не показывает 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


person Matthew Mitchell    schedule 30.11.2010    source источник


Ответы (2)


Просто удар в темноте, но из-за того, что вы сделали cell.textLabel.text = @"";, он мог уменьшить рамку textLabel до ширины 0. Попробуйте создать свои надписи на основе кадра, который, как вы знаете, имеет правильный видимый размер.

person David Liu    schedule 30.11.2010
comment
Хорошо, спасибо. Он действительно работает где-то еще, используя этот фрейм. Я попробую изменить это - person Matthew Mitchell; 30.11.2010

Чтобы показать свои 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, вы можете изменить число 25 в методе initWithFrame меток.
Надеюсь, это поможет вам :)

person matteodv    schedule 30.11.2010
comment
Спасибо, но, как вы можете видеть на изображении, мне нужно несколько ярлыков в строке. Кроме того, в вашем коде я бы использовал структуру if ... else или оператор switch. - person Matthew Mitchell; 01.12.2010