1

I want to increase tableview cell and tableview height based on content. Suppose tableview contain 2 record and his 1st cell height is 100 and 2nd cell height is 25 then tableview height should be 100+25=125. How to achieve this functionality?

Thanks in advance.

Monika Patel
  • 2,287
  • 3
  • 20
  • 45
  • What is your main goal, you want to calculate the height of table based on heights of all cells or you just want to create table which has different height cells? – Almas Adilbek Mar 04 '16 at 04:02
  • i just want to create height of cell based on label content & height of tableview based on cell height, this both. – Monika Patel Mar 04 '16 at 04:04
  • Use auto layout http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – Maverick Mar 04 '16 at 04:12
  • First you should get the cell inside the custom tableView cell.After that based on content you can set the cell height.Mainly here you need to set the label height and width. – user3182143 Mar 04 '16 at 04:58
  • By the way, why do you want to set table height based on cells height? it will scroll on both directions. Please explain further. – UIResponder Mar 04 '16 at 06:14

4 Answers4

2

You can definitely do that,

  1. First make sure your constraint of cells subView must set to top to bottom in order to calculate the height required for the cell.

  2. Make sure your delegated are set as below

    -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
          return 44;
    } 
    
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return UITableViewAutomaticDimension;
    }
    
  3. Set height constraint of your tableView and make outlet of that constraint.

  4. Add below method to your class where you want to resize your tableView dynamically.

    - (void)adjustHeightOfTableview
    {
    
        CGFloat height = self.tableView.contentSize.height;
       //CGFloat maxHeight = self.tableView.superview.frame.size.height - self.tableView.frame.origin.y;
    
       /* 
         Here you have to take care of two things, if there is only    tableView on the screen then you have to see is your tableView going below screen using maxHeight and your screen height,
         Or you can add your tableView inside scrollView so that your tableView can increase its height as much it requires based on the number of cell (with different height based on content) it has to display.
      */
    
      // now set the height constraint accordingly
      self.constraintHeightTableView.constant = height;
    
     //If you want to increase tableView height with animation you can do that as below.
    
     [UIView animateWithDuration:0.5 animations:^{
           [self.view layoutIfNeeded];
     }];
    }
    
  5. Call this method when you are ready with the dataSource for the table, and call the method as

    dispatch_async(dispatch_get_main_queue(), ^{
    
        [self.tableView reloadData];
    
        //In my case i had to call this method after some delay, because (i think) it will allow tableView to reload completely and then calculate the height required for itself. (This might be a workaround, but it worked for me)
        [self performSelector:@selector(adjustHeightOfTableview) withObject:nil afterDelay:0.3];
    });
    
Bharat Modi
  • 4,158
  • 2
  • 16
  • 27
1

If you are running iOS 8+, You can use:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 80 // your desired or expected height

properties.

  1. for this to take effect you should not have any height set in heightForRowAtIndexpath
  2. You should set the cell constraints i.e., constraints for the elements present inside cell, so the set constraints are enough for the tableviewcell to calculate it's height in run time
UIResponder
  • 785
  • 9
  • 15
0

Solution in swift 3

Step 1. Set the top, bottom, leading and trailing constraints (do not make it constant height, but do set a constant height constraint as of now). Now we gonna change this height dynamically based on the number of cells and its height.

Step 2. Drag an outlet of that height constraint to the class, and copy this function anywhere in the class.

    func adjustHeightOfTableview() {
    let height: CGFloat = tableview.contentSize.height
    self.outLetOfTheHeightConstraint.constant = height

}

Step 3.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    DispatchQueue.main.async {
            self.tableview.reloadData()
            self.perform(#selector(self.adjustHeightOfTableview))
        }
}
danu
  • 1,079
  • 5
  • 16
  • 48
-3
self.tableView.frame = CGRectMake(self.tableView.origin.x, self.tableView.origin.y, self.tableView.frame.size.width, 125);
Vin
  • 10,517
  • 10
  • 58
  • 71
petrhung9
  • 91
  • 7
  • i just explain example what i exactly want, i can't set static height like 125. – Monika Patel Mar 04 '16 at 04:07
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Tobi Nary Mar 04 '16 at 08:13
  • +1 for Jan Greve . I answered your question after you explained it :) So if you can't find solution, share me your contact – petrhung9 Mar 05 '16 at 04:57