how to set UITextView height dynamically inside a custom UITableViewCell with autolayout

autolayout, ios, objective-c, uitableview

Solution

Follow this answer: Link reference

Add a height constraint to your UITextView and create an outlet to your custom cell class.

Use `sizeThatFits:` on `UITextView` to get the size that fits the content and set this value as a `constant` of the constraint described in 2. Do this in `tableView:heightForRowAtIndexPath:`.

One caveat is that if there're many cells (hundreds or thousands) then you may hit performance issues.

Problem

I have UITableView, every tableViewCell is custom. Inside my customTableViewCell is a UITextView, TextViews frame is pin or same as its superView which is tableViewCell. How Am I gonna set the UITableViewCell height dynamically that is proportional to the TextViews size which will also depends to content text that I get from internet. (sample prototype) // this is how I manipulate every cell height ``` - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ // How do I set the height here, whats the best approach for this. with autolayout BTW } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // I initialize every cell here with my ArrayContainer, nothing much to refer here. } ```

Original source

Related problems