Failed to set label frame in tableView:cellForRowAtIndexPath: method

时光怂恿深爱的人放手 提交于 2019-12-08 07:55:07

问题


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;
}

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.)


回答1:


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:

and

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.

Hope that will work as you want.




回答2:


just uncheck the auto layout from the file inspector of the label frame.



来源:https://stackoverflow.com/questions/21909522/failed-to-set-label-frame-in-tableviewcellforrowatindexpath-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!