1

I have a UITableViewCell with multiple items inside. (Not just a textView so I cant follow this option.) I'm trying to dynamically size it's height based on the content it has inside.

The heights I will be changing, are a UITextView and a UIView. The textView will constantly be changing (at another method, if you'd like, I can post it). And the UIView will change if the user clicks a button:

Here is my code:

- (void)viewDidLoad
{
   self.tableView.rowHeight = UITableViewAutomaticDimension;
}

- (IBAction)thisButton:(id)sender
{   
    CGRect frame = self.myView.frame;
    frame.size.height = 50;
    frame.size.width = self.myView.frame.size.width;

    self.myView.frame = frame;

    // update 'myView's constraint
    self.viewHeight.constant = self.myView.frame.size.height;

    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [myTableView reloadData];
}

Problem:

  • What happens is, when I press the button, the UIView's height gets updated, but then everything else in the cell gets moved up, and the cell stays the same size.

  • When the UITextView's height changes, it doesn't pull everything else down, and the cells height stays the same. Though the textView's height does change and it just goes over everything else.

Constraints:

On the UITextView I have 3 constraints - 2 on each side, and 1 on top. The UIView has 3 constraints - 2 on each side, and 1 on the bottom. I then have a constraint connecting the UIView to the textView.

Community
  • 1
  • 1

1 Answers1

0

You can dynamically manage UITableViewCell size by calculating a max height of your internal views:

NSArray *array = [[NSArray alloc] initWithArray:@[view1, view2, view3]];

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    __block CGFloat maxHeight;
    [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIView *view = obj;
        if (view.bounds.size.height > maxHeight) {
            maxHeight = view.bounds.size.height;
        }
    }];

    return maxHeight;
}

Then in the end of your method that changes view's dimensions you must call [self.tableView reloadData]

Igor Leonovich
  • 458
  • 4
  • 13
  • I can use that, but I have multiple cells. –  Jan 27 '15 at 04:57
  • Exactly. Use [tableView cellForRowAtIndexPath:indexPath] for access to your different cells. See my update. I can answer more fully if you give me more detail code. – Igor Leonovich Jan 27 '15 at 08:51
  • I have an iboutlet Connection of UIViews. There are about 4 of them in the array. Do I have to make an if statement for every cell? cuz I cant assign myViewHeight with all the views. –  Jan 27 '15 at 19:34
  • Look, it is very simple. You make your myView resize and then call -'[self.tableView reloadData]'. While your tableView is updating it calls method '- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath' in which you asks new dimensions of your changed view. Here you can use max updated height of your views inside cell by your choice. Then you return it value and that is all. – Igor Leonovich Jan 27 '15 at 20:51
  • But how do I write this line CGFloat myViewHeight = [tableView cellForRowAtIndexPath:indexPath].myView.frame.size.height;" if myView an array of UIViews? It doesn't let me write that –  Jan 27 '15 at 20:53
  • All my cells had nothing as the height and it gave an error about nslayoutconstraints –  Jan 27 '15 at 21:16
  • My views are in a IBOutletCollection –  Jan 27 '15 at 21:16