user1688346
user1688346

Reputation: 1970

UITableView custom label set frame not working

I have a time label below a dynamic label - comment label, i want to place the time label beneath the comment label. But the time label is hidden when the comment label is larger. It looks like the Y offset of the time label doesn't change.

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;

Upvotes: 0

Views: 720

Answers (3)

Eric Loo
Eric Loo

Reputation: 1

if your rowheight is 100;so commentLabel=[[UILabel alloc]initWithFrame:CGRectMake(5.0,10.0, cell.contentView.frame.size.width-70, 50)];

and timeLabel must to do like this (can't use contentView.frame.size.height): timeLabel.frame = CGRectMake(5.0, 60.0 +10, cell.contentView.frame.size.width-70, 30)];

Upvotes: 0

Amalaric
Amalaric

Reputation: 655

try:

cell.clipsToBounds = NO;

Maybe the timeLabel have moved out the cellContent.

Upvotes: 1

Gazzini
Gazzini

Reputation: 738

Your problem is that this line:

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

Should be changed to this:

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

IMHO, this would make your life a lot easier.

Upvotes: 0

Related Questions