3

I am asking this question already knowing the answer is "no you can't" but in the hopes someone has a brilliant idea here we go.

I have a subclass of UITableViewCell that has a few different subviews one of witch is a UITextField that I have as user editable. So naturally the textField grows with the text that is entered.

Now the question is how could I get the tableview row to grow with the textField.

I have a variety of different size cells so I know how to use - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath but the problem is that I need to modify the after the cell is already in the tableview.

Also note that the previously noted delegate method gets called before the data source delegate method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

I have thought of just pulling up another view for the user to edit then putting that data into my data model then I would have to force the tableview to reload. (Can I reload just a certain cell/row)

Interestingly enough I think apple is doing what I want in their iTunes U app. When you tap a assignment it expands. I think they are using tableviews their right?

I know I have a lot of questions and talk here but I believe it is all related and just to show what I have researched. I am just looking for the possibility that one of you has a stroke of genius.

Hopefully this can help others also because this seems to be a hot topic but no one ask the question well or gets good answers.

Daniyar
  • 2,975
  • 2
  • 26
  • 39
Lucas Goossen
  • 527
  • 7
  • 15

1 Answers1

5

Actually, this has been asked and answered many times -- it's not impossible. Search for "UITableView custom row height" or "UITableView multi line UITextField" or similar and you'll find several well-answered questions.

You're on the right track -- you need to return a height in tableView:heightForRowAtIndexPath:, even though that method is called before you create/configure the cell. This is okay... you just need a way to compute that height without having the cell. Other answers reference -[NSString sizeWithFont:constrainedToSize:] and related methods... this should get you on the right track.

Changing the height while the text field is editing is less obvious, but this answer has the key... if you call

[tableView beginUpdates];
[tableView endUpdates];

the table view will not only ask its delegate for heightForRowAtIndexPath: again and resize the cell to match, it'll do it with a smooth animation.

Community
  • 1
  • 1
rickster
  • 124,678
  • 26
  • 272
  • 326