How to use UITextView in variable height UITableViewCell?

I am interested in creating a UITableViewCell with a UITextView inside it that adjusts its height (to a certain maximum) as the user enters more or less text into the UITextView. I know how to add a UITextView to a custom UITableViewCell, but how can I change the height of the UITableViewCell when the user is in the input process? Can this be done?

+3
source share
1 answer

As far as I know, the only way to set the table cell height is with the delegate method heightForRowAtIndexPath :. To call this method, you can call reloadData in the table, but I'm not sure what effect it will have on the UITextView you are editing. However, it is worth a try.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

return 100;
}

To calculate the return value, you will need to work out the height based on the current input and at the same time expand the text view. NSString has some useful methods for this, such as sizeWithFont: constrainedToSize: lineBreakMode :. He will develop CGSize, which you can use to create CGRect, etc.

+2
source

Source: https://habr.com/ru/post/1769220/


All Articles