Search code examples
iosuitableviewios7uilabel

Failed to set label frame in tableView:cellForRowAtIndexPath: method


The frame of the label in tableViewCell can not be set in the ableView:cellForRowAtIndexPath: method. In fact as show in the NSLog, the label's frame has changed but it did not show on the view. I expected the label(with green background) should be taller than in the picture, which can show all the text in it. The Prototype Cell with Identifier "cell" is created in the storyboard. Any help will be appreciated.

ps: Xcode 5.0.2, iOS7

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString * str = @"Do any additional setup after loading the view, typically from a nib Do any additional setup after loading the view, typically from a nib Do any additional setup after loading the view, typically from a nib";
    NSArray *array = [NSArray arrayWithObjects:str, str, str , str, str, str, nil];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row = indexPath.row;
    CGSize sizeConstraint = CGSizeMake(tableView.frame.size.width, 2000);
    CGSize size = [[array objectAtIndex:row] boundingRectWithSize:sizeConstraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont fontWithName:FontName size:FontSize]} context:nil].size;

    return size.height + 20;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row = indexPath.row;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    UILabel *label = (UILabel *)[cell.contentView viewWithTag:1];
    label.autoresizingMask = UIViewAutoresizingNone;
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.font = [UIFont fontWithName:FontName size:FontSize];
    label.backgroundColor = [UIColor greenColor];
    label.text = [array objectAtIndex:row];

    CGSize sizeConstraint = CGSizeMake(tableView.frame.size.width, 2000);
    CGSize size = [label.text boundingRectWithSize:sizeConstraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: label.font} context:nil].size;
    CGRect rect = label.frame;
    rect.size.height = size.height;
    [label setFrame:rect];
    NSLog(@"label height :%f", label.frame.size.height);
    [label sizeThatFits:sizeConstraint];
    return cell;
}

enter image description here enter image description here

when you scroll the tableView, you can see the label in the first and second cell are well displayed now. (Here i just scrolled out the two cells and pull them back.)

after scroll the tableView


Solution

  • In your post I don't find the definition of labelBible that you use to set the frame of the label.

    Using constraint it's quick:

    enter image description here

    and

    enter image description here

    In this way you don't have to do anything in your -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method.

    Edit: to use constraint: Use Autolayout has to be checked.

    enter image description here

    Hope that will work as you want.