0

I have a UITableView that has a number of static cells. There are no prototypes.

One cell as a UITextField and a UITextView. I want the UITextView to be hidden depending on the input of the UITextField. This is not a problem.

My problem is that I would like to resize the UITableViewCell that both of these controls are in - so that when the UITextView is hidden the cell height is 30, and when it is displayed its height is 70 (this makes the UITextView fully visible).

As they are static cells, I have simply set the height in IB to 70 (the largest) so when the UITextView is hidden there is a lot of unused space in the cell which I would like to get rid of by changing the cell height to 30.

I assume that I would need to tell the cell to redraw using something like

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates]; 

But how do I get the reference to the cell from a subview (the UITextField)?

Also how would I tell the cell the new height? Should I tag it and use

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

as I would for dynamic prototype sizing?

Szu
  • 2,254
  • 1
  • 21
  • 37
James Dawson
  • 948
  • 10
  • 12
  • 1
    Possible duplicate: http://stackoverflow.com/questions/11981071/dynamic-height-for-static-table-cells-with-wrapping-labels http://stackoverflow.com/questions/13560716/how-do-i-dynamically-resize-a-static-uitableviewcell – Szu Jul 04 '14 at 10:01
  • As you have written you can add tag equal to the selected row. You can also creat your CellTextField to keep NSIndexPath of UITableViewCell or keep weak reference to parent cell. – Szu Jul 04 '14 at 11:43
  • Hi, it's not a duplicate as i'm also asking how to get the reference to the cell when editing the text box - as answered in the marked answer. thanks for the replies! – James Dawson Jul 07 '14 at 07:19

2 Answers2

0

You can use a little trick. Create 2 iVars

1)  NSIndexPath *selectedIndex;
2)  BOOL isTextViewFull;

While toggling the Hide/Show textView

if(showfulltextview)
{
   selectedIndex  =  indexPath; // IndexPath of the current cell selected
   isTextViewFull =  YES;
}
else
{
   selectedIndex  =  nil;
   isTextViewFull =  NO;
}

[_yourTableView reloaddata];

Now in your delegate you can use those iVars

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CGFloat height = 30;
   if(isTextViewFull && indexPath.row == selectedIndex.row)
   {
     height = 70;
   }

  return height;
}

Here is a way to get your IndexPath for the TextField if you don't have the track of it. While editing the Textfield inside the Textfield Delegate

- (void)textFieldDidBeginEditing:(UITextField *)textField
  {
     // Get the cell in which the textfield is embedded
     id textFieldSuper = textField;
     while (![textFieldSuper isKindOfClass:[UITableViewCell class]]) {
           textFieldSuper = [textFieldSuper superview];
     }
     NSIndexPath *indexPath = [_yourTableView indexPathForCell:(UITableViewCell *)textFieldSuper];
   }
superGokuN
  • 1,399
  • 13
  • 28
  • thanks, this is the bit i was missing - the code to get the parent cell f the text box. thanks! – James Dawson Jul 07 '14 at 07:20
  • i've actually got another problem. As there are multiple static rows with different sizes per row (they are not a standard size, they are designed per row for their content) when i use height forrowatindexpath i need to return a height for ALL cells. i'll open a new question but i need to get the height of the cell as it is set in IB – James Dawson Jul 07 '14 at 09:57
  • @JamesDawson inside the delegate **heightForRowAtIndexPath** you can calculate the height for your content. For ex to get the size of your textView according to it's content refer this... http://stackoverflow.com/questions/23649162/uiscrollview-size-to-match-uitextview-size/23649391#23649391 – superGokuN Jul 07 '14 at 11:09
  • the cell doesnt have variable text content, the content is either a label with a single line AND a text area, OR a label with a single line only. i know how to size cells depending on text content from the model, but here i am making the cell larger/smaller depending on if a UITextVIew is hidden or not. – James Dawson Jul 09 '14 at 08:04
  • For ex: your calculated label height is 20 and your textView height with full text is 80 adding 5 margin at top and 5 at bottom, so total height becomes 20+80+5+5 = 110; Now only you have to do is go with the condition inside **heightForRowAtIndexPath** like .... height = 30; if(isTextViewFull && indexPath.row == selectedIndex.row){ height += 80; } retun height; Now the question is how to get 20(Label Height) or 80 (TextView height) for this you can refer the link in my above comment or you can google it :) – superGokuN Jul 09 '14 at 11:46
0

When it comes to dynamically changing the height of the tableviewcells.

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return Yourvalue; //return whatever u want ur cell to have size.
  }

use this method and return the value that u think ur cell should have, for instance, if ur label inside the cell is also created dynamically then, return the size of the label as per u created.

Hope this helps.

Saheb Roy
  • 5,899
  • 3
  • 23
  • 35