How to dynamically resize UITableViewCell

I have a classic grouped UITableView with editable UITextView inside each cell. These textual representations can be single or multi-line, and I want the cell to increase its height when the user writes, and the text starts a new line.

My question is: Do I need to reload the entire table in order to increase cell height? Is there no other method?

I searched a lot, and the previous answers and tutorials just talk about how to calculate text height, how to implement heightForRowAtIndexPath ... things I already know. My concern is that in order to achieve what I want, I will have to calculate the height and reload the table every time the user enters a new character, which I do not consider very clean or efficient.

Thank.

+55
iphone resize uitableview uitextview
Jun 18 '10 at
source share
4 answers

You do not always have to reload the entire table. Instead, you can just reload a single line.

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade]; 
+60
Jun 18 '10 at 13:57
source share
 [tableView beginUpdates]; [tableView endUpdates]; 
+78
Jan 14 2018-11-11T00:
source share

To be more specific, yes, you must implement tableView:heightForRowAtIndexPath: to calculate the new height, and then do as rickharrison says, and call [tableView reloadRowsAtIndexPaths:withRowAnimation] . Suppose your cells can have an increased height and normal height, and you want them to grow when pressed. You can do:

 -(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*) indexPath { if ([expandedPaths containsObject:indexPath]) { return 80; } else { return 44; } } -(void)tableView:(UITableView*) didSelectRowAtIndexPath:(NSIndexPath*) indexPath { [expandedPaths addObject:indexPath]; [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } 
+26
Jun 18 2018-10-18
source share

-reloadRowsAtIndexPaths:withRowAnimation did not resize the UITableViewCell even after I resized the cell frame. It only worked when I followed it with -reloadData :

 [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade]; [tableView reloadData]; 
+5
03 Oct
source share



All Articles