Change the Uitableviewcell Height According to Amount of Text

Change the UITableViewCell Height According to Amount of Text

Based on the code you have provided, I think you are increasing only the cell height and not the cell.textLabel's height.

Ideally, you should set the frame size of cell.textLabel and the cell for you to see the full text in the cell.

A neat way to see whats wrong with a view in terms of size, is to color it different than the background (try setting cell.textLabel background to yellow) and see if the height is actually being set.

Here's how it should be

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];

NSString *cellText = @"Go get some text for your cell.";
UIFont *cellFont = cell.textLabel.font;
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
cell.textlabel.frame.size = labelSize;
cell.text = cellText;
}

Hope this helps!

update: This is quite an old answer, and many lines in this answer may be deprecated.

How to change tableview cell height according to label text swift?

Considering you have already applied constraints on the label. And also set your number of lines to 0. And then paste the following code in viewDidLoad method

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

Dynamically change cell height according to label text

The bottom anchor needs to be set:

cevapLabel.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true

UITableViewCell auto height based on amount of UILabel text

To go a little further on Dao's answer..

He is correct, you need to use UITableViewAutomaticDimension

Also, you need to ensure that you setup your constraints in a way that all of the content in the cell is constrained to the cells contentView. So your label will likely need constraints such as

  • Leading constraint to ImageView
  • Top constraint to contentView
  • Bottom constraint to contentView
  • Trailing constraint to contentView

Make sure that you set the UILabel to multiline (or lines = 0) and it should work.

If you are using the heightForRowAt delegate functions ensure you return UITableViewAutomaticDimension

Adjust UITableViewCell height according to the resizing label

In your case you've to manually manage autolayout constraints when only button is visible and after clicking text will be visible.
I've prepared a demo for that which you can found here.

https://github.com/rushisangani/TableViewCellExpand

Please refer storyboard constraints for TableViewCell and manage that wisely.

Changing UILabel text length in UITableViewCell with dynamic height does not resize it correctly

I have found a solution that works, after updating the UILabel content I am calling:

 UIView.animateWithDuration(0.3) {
cell.contentView.layoutIfNeeded()
}

To update cell constraints. And then to resize the cell height:

 tableView.beginUpdates()
tableView.endUpdates()

I will wait to accept this answer to see if someone else will answer a more elegant solution though.



Related Topics



Leave a reply



Submit