2

I have an UITableView which includes a list of UITableViewCells. And I set the heights of UITableViewCells in method (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath.

However, I need to increase the height of UITableViewCell when it is selected. For example, the height of UITableViewCell needs to increase 100 pixels when the cell is selected, does anyone know how to do it?

TryTryAgain
  • 7,632
  • 11
  • 46
  • 82
Michael C
  • 23
  • 1
  • 3

2 Answers2

11
  1. Store selected index in your controller.
  2. Return cell's height depending on that index:

    CGFloat height = 30.0f;
    ...
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return indexPath.row == selectedIndex ? height+100 : height;
    }
    
  3. Refresh tableview geometry in didSelectRowAtIndexPath method (empty block between beginUpdates and endUpdates does the trick):

    - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        index = indexPath.row;
        [tableView beginUpdates];
        [tableView endUpdates];      
    }
    
Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • Shouldn't he reload that cell? – Eiko Aug 22 '10 at 16:32
  • You need to reload the cell only if its contents change. on practice it must be so indeed... – Vladimir Aug 22 '10 at 16:40
  • Actually in that case he needs to reload 2 cells - the newly selected and the previous one – Vladimir Aug 22 '10 at 16:42
  • Nice. I got the same confusion, and first I try the [tableview reloaddata] method to refresh geometry which takes seconds. beginUpdates/endUpdates is the better and right way. – fayhot Mar 22 '14 at 04:40
0

Make your delegate method

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

return the (new) correct height and make the table view reload the given path.

Eiko
  • 25,601
  • 15
  • 56
  • 71