UITableView自定义标签集框架无法正常工作

时间:2014-04-18 03:19:13

标签: ios

我在动态标签下面有一个时间标签 - 评论标签,我想放置 评论标签下方的时间标签。但是在评论标签时隐藏了时间标签 更大看起来时间标签的Y偏移量不会改变。

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
spComments *comment=[_data objectAtIndex:indexPath.row];
UILabel *timeLabel;
UILabel *commentLabel;

//do not recreate controls, just change the contents each time
if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    double likeHeight=40;
    double likeWidth=40;
    commentLabel=[[UILabel alloc]initWithFrame:CGRectMake(5.0,10.0,
                                                                   cell.contentView.frame.size.width-70, cell.contentView.frame.size.height)];



    [commentLabel setFont:self.font];


    [cell.contentView addSubview:commentLabel];
    [commentLabel setTag:1]; //have to set it after added


    //like button
    UIButton *likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [likeButton setFrame:CGRectMake(cell.contentView.frame.size.width-40, 10, likeWidth, likeHeight)];
    //[likeButton setImage:[UIImage imageNamed:@"heart_icon_small"] forState: UIControlStateNormal];
    [likeButton setBackgroundImage:[UIImage imageNamed:@"heart_icon_small"] forState:UIControlStateNormal];
    // likeButton.imageEdgeInsets = UIEdgeInsetsMake(-50, 0, 0, 0);
    [cell.contentView addSubview:likeButton];

    timeLabel=[[UILabel alloc]init];
    UIFont *timeFont=[UIFont fontWithName:@"AppleSDGothicNeo-Light" size:10];
    [timeLabel setFont:timeFont];
    [cell.contentView addSubview:timeLabel];
    [timeLabel setTag:2];



}

commentLabel = (UILabel*)[cell.contentView viewWithTag:1];


[commentLabel setText:comment.message];
[commentLabel adjustLabel];
NSLog(@"Comment height %f",commentLabel.frame.size.height);
[commentLabel setBackgroundColor:[UIColor redColor]];

timeLabel = (UILabel*)[cell.contentView viewWithTag:2];
[timeLabel setFrame:CGRectMake(5.0, commentLabel.frame.size.height+10, 100, 50)];
[timeLabel setText:comment.sincewhen];
[timeLabel setBackgroundColor:[UIColor blueColor]];


return cell;

3 个答案:

答案 0 :(得分:1)

尝试:

cell.clipsToBounds = NO;

也许timeLabel移出了cellContent。

答案 1 :(得分:0)

你的问题是这一行:

[timeLabel setFrame:CGRectMake(5.0, commentLabel.frame.size.height+10, 100, 50)];

应改为:

[timeLabel setFrame:CGRectMake(5.0, commentLabel.frame.size.height + commentLabel.frame.origin.y + 10, 100, 50)];
恕我直言,this会让你的生活更轻松。

答案 2 :(得分:0)

如果你的行高是100;那么 commentLabel=[[UILabel alloc]initWithFrame:CGRectMake(5.0,10.0, cell.contentView.frame.size.width-70, 50)];

和timeLabel必须这样做(不能使用contentView.frame.size.height): timeLabel.frame = CGRectMake(5.0, 60.0 +10, cell.contentView.frame.size.width-70, 30)];