简体   繁体   中英

Changing UILabel frame height inside tableView cell

I get my UILabel text from remote url, and sometimes the text is too short/too long and I need to adapt my frame size to the text height. I have implemented costume cell using xib .

屏幕截图

This is what I tried so far, which supposed to works, but don't work for a reason I can't understand:

 cell.fbLastObject.text = [NSString stringWithFormat:@"%@",update.fbLastObject];
 [cell.fbLastObject sizeToFit];

 //Cell Size
 CGSize labelSize = [cell.fbLastObject.text sizeWithFont:cell.fbLastObject.font
                                       constrainedToSize:cell.fbLastObject.frame.size
                                           lineBreakMode:NSLineBreakByWordWrapping];
 labelHeight = labelSize.height;
 cell.fbLastObject.frame = CGRectMake(0,0,300,labelHeight);

Anyone has any idea?

For iOS 8 and later , you can use UITableViewAutomaticDimension Put the below code in the viewDidLoad is:

  tableView.rowHeight = UITableViewAutomaticDimension;
  /* any estimated height but must be more than 2 */
  tableView.estimatedRowHeight = 160.0;
  tableview.delegate = self;
  tableview.dataSource = self;
 // you have created the SimpleTableViewCell.xib, So you need to register that cell with table.
  UINib *nib = [UINib nibWithNibName:@"SimpleTableViewCell" bundle:nil];
  [tableview registerNib:nib forCellReuseIdentifier:@"SimpleTableViewCell"];

Now add proper cell fbLastObject constraints (top, bottom, leading and trailing) pins to super View & set the number of lines to 0

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"SimpleTableViewCell";

    SimpleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    // here set text for your cell label 
    cell.fbLastObject.text = @"My Text";

    [cell setNeedsLayout];
    [cell layoutIfNeeded];

    return cell;
}

You can try self.tblView.rowHeight = UITableViewAutomaticDimension;

For dynamic cell using autolayout

You can do by mapping Height Constraint of UILabel and calculate the size of the text from remote url and according to that calculated height return height of row and also give that height to mapped Height Constraint outlet and before return cell just write :-

[cell.UILabel updateConstraints];

In your question the steps you took to get the size for the Label is fine. Make Label's property SizeToFit and NumberOfLines to 0 and add the appropriate constraints to it. Then create an instance of NSLayoutConstraint for Height constraint of Label and make it attach to bottom of UITableViewCell. Then when ever you have the CGSize from text then you need to call UpdateConstraintIfNeeded on self and then need to set height constant property for that Label's constraint & need to set the heightForRowAtIndexpath of UITableVIew. Hope this will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM