21
votes

iOS 8 introduced a way for tableViews to automatically adjust their cell's height based on their content (via AutoLayout).

// in viewDidLoad:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44.0

I already got it working with labels, images, etc.

However, I can not figure out a way for the table view cell to grow automatically as soon as a cell's text view changes?

Some more information on the setup: The UITextView inside a cell has horizontal and vertical constraints to the table view cell's contentView. I also disabled scrolling for the text view.

I have also tried to change the height constraint of the cell manually, but the cell would not adopt those changes as well.

6
any progress on that one? I am looking to do the same... Are you trying to get it to work while the user types? Or is the UITextView you are using not user-editable?Georg

6 Answers

24
votes

If everything is set up properly (Auto Layout constraints) you do not have to calculate anything yourself.

All you have to do is disable UITextView's scrolling enabled property then on textViewDidChange call tableView.beginUpdates() and tableView.endUpdates().

For a detailed explanation, check out a post I wrote which also includes a working sample project.

4
votes

Reload the cell in the textViewDidBeginEditing: method of your UITextViewDelegate

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForItem:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];

Obviously you should change the indexpath to the correct one for your cell. This will get the cell to update its constraints and resize itself.

2
votes

The tableView has to be informed that the textView has changed. Other posts usually answer a "static" textView problem. However, if you are typing inside a table, the cell needs to grow as the textView grows as you type. The textView grows by disabling scrolling. The cell grows by setting the textView's top and bottom constraints to those of the contentView of the cell. This will work once, once the table loads. However to tell the tableView to grow in real time, you have to do in the textView's didChange delegate call

func textViewChanged(onCell cell: YourCustomCell) {
    UIView.setAnimationsEnabled(false)
    tableView.beginUpdates()
    tableView.endUpdates()
    UIView.setAnimationsEnabled(true)
}

This will work as you expect. The cell will grow as you type and there won't be a "bouncing".

1
votes

Solution for Swift 4

func textViewDidChange(_ textView: UITextView) {
    let fixedWidth = textView.frame.size.width
    let oldSize = textView.frame.size
    let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT)))
    if oldSize.height != newSize.height {
        UIView.setAnimationsEnabled(false)
        var newFrame = textView.frame
        newFrame.size = CGSize(width: fmax(newSize.width, fixedWidth), height: newSize.height)
        textView.frame = newFrame
        self.tableView.beginUpdates()
        self.tableView.endUpdates()
        UIView.setAnimationsEnabled(true)
    }
}
0
votes

This is the function I use, which solves the problem of the table view bouncing back after every keystroke.

 - (void)textViewDidChange:(UITextView *)textView {

    CGFloat fixedWidth = textView.frame.size.width;
    CGSize oldSize = textView.frame.size;
    CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
        // Resize cell only when cell's size changes, to prevent bouncing back and forth.
        if (oldSize.height != newSize.height) {
        [UIView setAnimationsEnabled:NO];
        CGRect newFrame = textView.frame;
        newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
        textView.frame = newFrame;
        [self.tableView beginUpdates];
        [self.tableView endUpdates];
        [UIView setAnimationsEnabled:YES];
        }
    }
0
votes

You should disable scroll from your UITextView