Resize UITableView line height without keyboard deviation

I have a TextView inside a UITableViewCell. The text inside the TextView can be edited where it is.

A TextView grows and shrinks vertically depending on the number of lines contained in the text. So far, I have not found the ability to also let the string containing the TextView grow and dynamically shrink.

[tableView reloadData]; 

or

 [tableView reloadRowsAtIndexPaths: withRowAnimation:]; 

do not work because they reject the keyboard with every change.

Is there a way to change the height of a single row in a UITableView without deviating the keyboard?

+12
ios objective-c height uitableview row
Mar 17 '11 at 19:32
source share
3 answers

Yes, you can adjust the height of the lines as you type. This is not documented, and it challenges the reason, but all you have to do is set a new value in tableView.rowHeight (or have heightForRowAtIndexPath: ready to calculate new heights), and then do this:

 [tableView beginUpdates]; // updates the row heights ... [tableView endUpdates]; // ... nothing needed in between 
+30
Apr 14 2018-11-11T00:
source share

After reloadRowsAtIndexPaths, the cell changes the memory value, so if you call startFirstResponder immediately after reloadRowsAtIndexPaths, you try to set an old cell that does not exist as firstResponder (startFirstResponder returns false and keyboard outputs).

The solution is to call cellForRowAtIndexPath again to get the new memory value of the reloaded cell, and then call the getFirstResponder function for that cell.

0
Mar 30 2018-11-11T00:
source share

The challenge is to cause the row height to be recalculated without reusing cells (to save the current responder).

You can call moveRowAtIndexPath:toIndexPath: for the same indexPath immediately after changing the height

 - (void)textInputTableView:(UITableView *)tableView cellHeightDidChangeForIndexPath:(NSIndexPath *)indexPath { [tableView moveRowAtIndexPath:indexPath toIndexPath:indexPath]; } 
0
Jun 03 '16 at 21:20
source share



All Articles