How to Resize Uitableviewcell to Fit Its Content

How to resize UITableViewCell to fit its content?

You need you implement heightForRowAtIndexPath.

Say that the data that is to be displayed in the textView is stored in a NSArray.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat cellheight = 30; //assuming that your TextView's origin.y is 30 and TextView is the last UI element in your cell

NSString *text = (NSString *)[textArray objectAtIndex:indexpath.row];
UIFont *font = [UIFont systemFontOfSize:14];// The font should be the same as that of your textView
CGSize constraintSize = CGSizeMake(maxWidth, CGFLOAT_MAX);// maxWidth = max width for the textView

CGSize size = [text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

cellHeight += size.height; //you can also add a cell padding if you want some space below textView

}

Resizing UITableView to fit content

Actually I found the answer myself.

I just create a new CGRect for the tableView.frame with the height of table.contentSize.height

That sets the height of the UITableView to the height of its content.
Since the code modifies the UI, do not forget to run it in the main thread:

dispatch_async(dispatch_get_main_queue(), ^{
//This code will run in the main thread:
CGRect frame = self.tableView.frame;
frame.size.height = self.tableView.contentSize.height;
self.tableView.frame = frame;
});

How to resize UITableView width to fit its content?

  1. Set a width auto-layout constraint to the table view.
  2. Set an outlet from the constraint to your code (ctrl+drag, just like a view).
  3. When you have the size of the label (if the label also has auto-layout then you will have it at viewDidLayoutSubviews) set the width to the table like so:

    - (void)viewDidLayoutSubviews {
    self.myTableWidthConstraint.constant = self.myLabel.frame.size.width;
    [self.myTableView updateConstraints];
    [self.myTableView layoutIfNeeded];
    }

How do I tell my UITableViewCell to auto-resize when its content changes?

There is a documented way to do this. See UITableView.beginUpdates() documentation:

You can also use this method followed by the endUpdates method to animate the change in the row heights without reloading the cell.

So, the correct solution is:

tableView.beginUpdates()
tableView.endUpdates()

Also note that there is a feature that is not documented - you can add a completion handler for the update animation here, too:

tableView.beginUpdates()
CATransaction.setCompletionBlock {
// this will be called when the update animation ends
}

tableView.endUpdates()

However, tread lightly, it's not documented (but it works because UITableView uses a CATransaction for the animation).

Making a UITableViewCell resize to fit different screen sizes

I ended up figuring this out with the help of @schmidt9 's and @VinodVishwanath 's post. In order to properly calculate the size of the screen that you would like content to be in, you have to subtract the space of the navigation bar along with the Status bar (time, battery etc.) The following lines can carry this out:

To find the size of the Navigation bar:

self.navigationController!.navigationBar.frame.size.height

To find the size of the total screen including Nav bar and status menu:

UIScreen.mainScreen().fixedCoordinateSpace.bounds.height

The status bar is consistently 20 across all devices, so you can simply subtract it. You are finally left with this code:

let screenHeight = UIScreen.mainScreen().fixedCoordinateSpace.bounds.height - self.navigationController!.navigationBar.frame.size.height - 20
self.tableView.rowHeight = screenHeight / 3

To put it in more of a mathematical equation:
Total screen - navigation bar - 20 (from status bar) = total workspace

Hope this helps, please comment for any further instruction.

Resizing UITableViewCell to fit Content Performance

The way I had implemented adding the constraints, the constraints were updated whenever [self updateViewConstraints] was called, regardless of whether or not the cell had already had its constraints calculated and applied. I resolved this by adding a BOOL property to my custom TableViewCell called didUpdateConstraints, and setting it to YES as soon as constraints are updated the first time. Then in my [self updateViewConstraints] method I only update the constraints if !self.didUpdateConstraints

Now that constraints are not being needlessly updated when all views have already been correctly constrained, the performance is significantly better, and I don't observe any slowdown upon scrolling through the table.

How to support auto resizing UITableViewCell with a UILabel in it

Here is what you need to do:

  1. Need to use auto layout in your cell and set up appropriate constraints for UILabel. Since we want to resize the UILabel to fit the text set to it, we will set the number of lines value to 0. And then set the leading, trailing, bottom and top constraints to the UILabel. Please see screenshots here:

    Lines as 0
    Sample Image

  2. In your Table View controller > viewDidLoad call these two lines:

      tableView.estimatedRowHeight = 50.0  //Give an approximation here
    tableView.rowHeight = UITableViewAutomaticDimension

    and also implement delegate method:

     override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
    return UITableViewAutomaticDimension
    }


Related Topics



Leave a reply



Submit