Change UITableView height dynamically Change UITableView height dynamically ios ios

Change UITableView height dynamically


There isn't a system feature to change the height of the table based upon the contents of the tableview. Having said that, it is possible to programmatically change the height of the tableview based upon the contents, specifically based upon the contentSize of the tableview (which is easier than manually calculating the height yourself). A few of the particulars vary depending upon whether you're using the new autolayout that's part of iOS 6, or not.

But assuming you're configuring your table view's underlying model in viewDidLoad, if you want to then adjust the height of the tableview, you can do this in viewDidAppear:

- (void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];    [self adjustHeightOfTableview];}

Likewise, if you ever perform a reloadData (or otherwise add or remove rows) for a tableview, you'd want to make sure that you also manually call adjustHeightOfTableView there, too, e.g.:

- (IBAction)onPressButton:(id)sender{    [self buildModel];    [self.tableView reloadData];    [self adjustHeightOfTableview];}

So the question is what should our adjustHeightOfTableview do. Unfortunately, this is a function of whether you use the iOS 6 autolayout or not. You can determine if you have autolayout turned on by opening your storyboard or NIB and go to the "File Inspector" (e.g. press option+command+1 or click on that first tab on the panel on the right):

enter image description here

Let's assume for a second that autolayout was off. In that case, it's quite simple and adjustHeightOfTableview would just adjust the frame of the tableview:

- (void)adjustHeightOfTableview{    CGFloat height = self.tableView.contentSize.height;    CGFloat maxHeight = self.tableView.superview.frame.size.height - self.tableView.frame.origin.y;    // if the height of the content is greater than the maxHeight of    // total space on the screen, limit the height to the size of the    // superview.    if (height > maxHeight)        height = maxHeight;    // now set the frame accordingly    [UIView animateWithDuration:0.25 animations:^{        CGRect frame = self.tableView.frame;        frame.size.height = height;        self.tableView.frame = frame;        // if you have other controls that should be resized/moved to accommodate        // the resized tableview, do that here, too    }];}

If your autolayout was on, though, adjustHeightOfTableview would adjust a height constraint for your tableview:

- (void)adjustHeightOfTableview{    CGFloat height = self.tableView.contentSize.height;    CGFloat maxHeight = self.tableView.superview.frame.size.height - self.tableView.frame.origin.y;    // if the height of the content is greater than the maxHeight of    // total space on the screen, limit the height to the size of the    // superview.    if (height > maxHeight)        height = maxHeight;    // now set the height constraint accordingly    [UIView animateWithDuration:0.25 animations:^{        self.tableViewHeightConstraint.constant = height;        [self.view setNeedsUpdateConstraints];    }];}

For this latter constraint-based solution to work with autolayout, we must take care of a few things first:

  1. Make sure your tableview has a height constraint by clicking on the center button in the group of buttons here and then choose to add the height constraint:

    add height constraint

  2. Then add an IBOutlet for that constraint:

    add IBOutlet

  3. Make sure you adjust other constraints so they don't conflict if you adjust the size tableview programmatically. In my example, the tableview had a trailing space constraint that locked it to the bottom of the screen, so I had to adjust that constraint so that rather than being locked at a particular size, it could be greater or equal to a value, and with a lower priority, so that the height and top of the tableview would rule the day:

    adjust other constraints

    What you do here with other constraints will depend entirely upon what other controls you have on your screen below the tableview. As always, dealing with constraints is a little awkward, but it definitely works, though the specifics in your situation depend entirely upon what else you have on the scene. But hopefully you get the idea. Bottom line, with autolayout, make sure to adjust your other constraints (if any) to be flexible to account for the changing tableview height.

As you can see, it's much easier to programmatically adjust the height of a tableview if you're not using autolayout, but in case you are, I present both alternatives.


create your cell by xib or storyboard. give it's outlet's contents.now call it in CellForRowAtIndexPath. eg. if you want to set cell height according to Comment's label text.enter image description here

so set you commentsLbl.numberOfLine=0;enter image description here

so set you commentsLbl.numberOfLine=0;

then in ViewDidLoad

 self.table.estimatedRowHeight = 44.0 ;self.table.rowHeight = UITableViewAutomaticDimension;

and now

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{return UITableViewAutomaticDimension;}


Lots of the answers here don't honor changes of the table or are way too complicated. Using a subclass of UITableView that will properly set intrinsicContentSize is a far easier solution when using autolayout. No height constraints etc. needed.

class UIDynamicTableView: UITableView{    override var intrinsicContentSize: CGSize {        self.layoutIfNeeded()        return CGSize(width: UIViewNoIntrinsicMetric, height: self.contentSize.height)    }    override func reloadData() {        super.reloadData()        self.invalidateIntrinsicContentSize()    }} 

Set the class of your TableView to UIDynamicTableView in the interface builder and watch the magic as this TableView will change it's size after a call to reloadData().