Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. enter image description here

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)

enter image description here

// 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.
}
like image 918
Bordz Avatar asked Dec 04 '13 07:12

Bordz


1 Answers

  1. Follow this answer: Link reference

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

  3. 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.

like image 99
Arek Holko Avatar answered Nov 06 '22 08:11

Arek Holko