59

I have the classical grouped UITableView with editable UITextViews inside every cell. This text views can be single or multi-lined, and I want the cell to increase its height as the user writes and the text starts a new line.

My question is: do I need to reload the whole table just to increase the height of a cell? Isn't there any other method?

I've been searching a lot, and previous answers and tutorials just talk about how to calculate text height, how to implement heightForRowAtIndexPath... things that I already know. My concern is that, to achieve what I want, I will have to calculate height and reload the table every time the user enters a new character, which I don't find very clean or efficient.

Thanks.

4 Answers 4

94
[tableView beginUpdates];
[tableView endUpdates];
4
  • 15
    give it a try, this code forces UITableView to reload cell sizes only, but not cell contents. Jun 28, 2011 at 11:43
  • While this basically works, it does not produce as smooth of an animation if you grow the cell as reloading the cell with an automatic row transition. Feb 22, 2012 at 4:08
  • 1
    Great.. Use it along with reloadRowsAtIndexPaths if you want an animation at some of your rows. May 8, 2013 at 6:12
  • I added this code in 'shouldChangeTextIn'. and works fine. But not working for new line, and it grows after type a character Nov 29, 2018 at 6:23
63

You do not always have to reload the entire table. You can instead just reload that one row.

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
8
  • Thanks! But I have another problem now: I just want to reload that row to increase its size, I don't want the cell content to be instanced again. However, dequeueReusableCellWithIdentifier:@"customid" only starts reusing after second call. What could I do?
    – araid
    Jun 22, 2010 at 11:55
  • @rickharrison actually you can, check out my answer below. Oct 25, 2012 at 8:16
  • It's doing the job really nice, but the cell's content grows respectively. How can I avoid this?
    – Gal
    Apr 23, 2013 at 8:24
  • 3
    I'm having a big problem with this... I have a TextField in my cell and calling reload(anything) will cause my textfield to resign first responder status. Any ideas?
    – Daniel T.
    May 13, 2013 at 14:53
  • 1
    Programmatically save your IndexPath of your row and set becomeFirstResponder to your TextField
    – Sn0wfreeze
    Sep 2, 2014 at 20:58
27

To be more specific, yes, you have to implement tableView:heightForRowAtIndexPath: to calculate the new height, then do as rickharrison says and call [tableView reloadRowsAtIndexPaths:withRowAnimation]. Lets say your cells can have an expanded height and a normal height, and you want them to grow when tapped on. 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];
}
4

-reloadRowsAtIndexPaths:withRowAnimation did not resize the UITableViewCell height, even after I changed the Cell's frame. It only worked when I followed it with a -reloadData:

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadData];
1
  • Maybe you were referencing the wrong indexPath, if you debug the application you clearly see that after you call tableView reloadRowsAtIndexPath: the method tableView:heightForRowAtIndexPath is in fact called. Apr 8, 2015 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.