9

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.
}
neowinston
  • 7,584
  • 10
  • 52
  • 83
Bordz
  • 2,810
  • 4
  • 23
  • 27
  • possible duplicate of [Using Auto Layout in UITableView for dynamic cell layouts & heights](http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-heights) – ilya n. Dec 04 '13 at 07:44

3 Answers3

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

Community
  • 1
  • 1
Arek Holko
  • 8,966
  • 4
  • 28
  • 46
  • @Spail: can you check if the correct size is returned from the call to `sizeThatFits:`? – Arek Holko May 11 '14 at 09:26
  • yeah, the size returned is kinda strange. its width is larger than it should be. can you post your code for this? – Spail May 11 '14 at 12:15
  • Nevermind, I forgot to add +1 to height to account for the cell separator. Seems like it works now. Thanks. – Spail May 11 '14 at 13:52
1

Primarily get the texts as NSArray and Assign it into the UITextView's as temporary, So that you will get the Height of Cell :

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        CGRect frame;
        //  set the height here, According to the NSArray of text 
        if(textView || section )
        UITextView *tempTxtView = [[UITextView alloc] init];
        tempTxtView.text = // Add the Text of index according to the Section
        // Fit  the  UITextView to the Content 
        frame = tempTxtView.frame;
        frame.size.height = tempTxtView.contentSize.height;
        tempTxtView.frame = frame;
    }    
    return frame.size.height;
 }

Once got the UITableViewCell height : Then No need to worry about this, right ?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// I initialize every cell here with my ArrayContainer, nothing much to refer here.

self.textView =  [[UItextView alloc] initWithFrame:CGRectMake(cell.frame.origin.x +20, cell.frame.origin.y +10, cell.frame.size.width - 20, cell.frame.size.height - 20)];

// ....... Do further with the NSArray of Text


}

Note : I Haven't Tested, Its just a thought. Hope this will help you

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
0
 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
      CGSize titlesize = [TxtValue.text sizeWithFont:[UIFont fontWithName:appdel.strFont size:25.0f] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];  /// ----- -set width as per your requirement inplace of 300 

      return titlesize.height+10;
 }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

}
Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38