在自定义UITableView单元格中重叠UILabel

时间:2012-01-14 19:28:18

标签: iphone objective-c xcode4 uitableview

我在自定义单元格中重叠标签时遇到问题。 以下是此单元格的init和layout方法:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self)
    {
        self.img = [[[UIImageView alloc] init] autorelease];
        [self.contentView addSubview:self.img];

        self.p = [[[UILabel alloc] init] autorelease];
        [self.p setTextAlignment:UITextAlignmentCenter];
        [self.p setFont:[UIFont fontWithName:@"Helvetica" size:10.0]];
        [self.p setLineBreakMode:UILineBreakModeTailTruncation];
        [self.p setNumberOfLines:1];
        [self.p setTextColor:[UIColor whiteColor]];
        [self.p setBackgroundColor:[UIColor redColor]];
        [self.p setText:@"PREMIERA"];
        [self.contentView addSubview:self.p];        

        self.g = [[[UILabel alloc] init] autorelease];
        [self.g setTextAlignment:UITextAlignmentLeft];
        [self.g setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12.0]];
        [self.g setLineBreakMode:UILineBreakModeTailTruncation];
        [self.g setNumberOfLines:1];
        [self.contentView addSubview:self.g];

        self.t = [[[UILabel alloc] init] autorelease];
        [self.t setTextAlignment:UITextAlignmentLeft];
        [self.t setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14.0]];
        [self.t setMinimumFontSize:12.0f];
        [self.t setAdjustsFontSizeToFitWidth:YES];
        [self.t setLineBreakMode:UILineBreakModeTailTruncation];
        [self.t setNumberOfLines:1];
        [self.contentView addSubview:self.t];

        self.o = [[[UILabel alloc] init] autorelease];
        [self.o setTextAlignment:UITextAlignmentLeft];
        [self.o setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];
        [self.o setLineBreakMode:UILineBreakModeTailTruncation];
        [self.o setNumberOfLines:1];
        [self.contentView addSubview:self.o];
    }

    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.img.frame = CGRectMake(0, 0, 40, 60);
    self.p.frame = CGRectMake(0, 0, 0, 0);
    self.g.frame = CGRectMake(0, 0, 0, 0);
    self.t.frame = CGRectMake(0, 0, 0, 0);
    self.o.frame = CGRectMake(0, 0, 0, 0);

    CGSize pSize = CGSizeMake(0, 0);
    CGSize gSize = CGSizeMake(0, 0);
    CGSize tSize = [self.t.text sizeWithFont:self.t.font constrainedToSize:CGSizeMake(230, 16) lineBreakMode:self.t.lineBreakMode]; 
    CGSize oSize = [self.o.text sizeWithFont:self.o.font constrainedToSize:CGSizeMake(230, 16) lineBreakMode:self.o.lineBreakMode];


    if (self.jestPremiera)
    {
        pSize = [self.p.text sizeWithFont:self.p.font constrainedToSize:CGSizeMake(60, 16) lineBreakMode:self.p.lineBreakMode];    
        gSize = [self.g.text sizeWithFont:self.g.font constrainedToSize:CGSizeMake(175, 16) lineBreakMode:self.g.lineBreakMode];
        self.p.frame = CGRectMake(50, 5, 60, pSize.height);
        self.p.hidden = NO;
        self.g.frame = CGRectMake(115, 5, 175, gSize.height);
    }
    else
    {
        gSize = [self.g.text sizeWithFont:self.g.font constrainedToSize:CGSizeMake(230, 16) lineBreakMode:self.g.lineBreakMode];            
        self.p.hidden = YES;
        self.g.frame = CGRectMake(50, 5, 230, gSize.height);
    }

    self.t.frame = CGRectMake(50, 5 + gSize.height, 230, tSize.height);
    self.o.frame = CGRectMake(50, 5 + gSize.height + tSize.height, 230, oSize.height);            
}

通常这有效,但有时(仅限3G设备)看起来像:http://imageshack.us/f/17/tvkino1.jpg/

任何人都可以告诉我这种行为的原因是什么,我该怎么办?

2 个答案:

答案 0 :(得分:0)

请尝试使用NSLog();

调试这些标签的框架

答案 1 :(得分:0)

如果您在分配这些标签时使用标签号定义每个标签,我认为最佳解决方案。现在,当您需要这些标签时,只需查看这些标签......

   yourlabelname = (UILabel *)[cell.contentView viewWithTag:yourLabelTagNumber];

并在-(void)viewDidAppear中重新加载tableview;