How to make UITableView row dynamically grow with user modified content

I ask this question already knowing that the answer is “no, you can't”, but in the hope that someone has a brilliant idea, we go.

I have a subclass of UITableViewCell that has several different subheadings, one of which is UITextField, which I have as user editable. So naturally the textField grows with the entered text.

Now the question is how can I get the tableview row to grow using a textField.

I have different size cells, so I know how to use - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

, but the problem is I need to change the value after the cell is already in the table view.

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

I thought about just bringing up another view for the user to change, and then putting that data into my data model, then I would have to force reload the tableview. (Can I reload only a specific cell / row)

Interestingly, I think apple does what I want in their iTunes U app. When you click on an assignment, it expands. I think they are using tables in their rights?

I know I have a lot of questions and will talk here, but I believe this is all related and just to show what I have researched. I'm just looking for an opportunity for one of you to have a genius.

Hopefully this can help other people as well, because it seems to be a hot topic, but nobody asks the question well or gets good answers.


source to share

1 answer

Actually, they asked about this and answered many times - it's not impossible. Search for "UITableView custom height height" or "UITableView multi line UITextField" or similar, and you will find some well-answered questions.

You are on the right track - you need to return the height in tableView:heightForRowAtIndexPath:

even if this method is called before creating / setting the cell. That's ok ... you just need a way to calculate that height without a cell. The other answers link to -[NSString sizeWithFont:constrainedToSize:]

and related methods ... that should get you on the right track.

Changing the height when editing a textbox is less obvious, but this answer has a clue ... if you call

[tableView beginUpdates];
[tableView endUpdates];


the table view will not only ask its delegate again for heightForRowAtIndexPath:

and resize the cell to fit, it will do so with a smooth animation.



All Articles