views:

97

answers:

2

Hi,

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?

+1  A: 
  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
Shouldn't he reload that cell?
Eiko
You need to reload the cell only if its contents change. on practice it must be so indeed...
Vladimir
Actually in that case he needs to reload 2 cells - the newly selected and the previous one
Vladimir
thanks a lot, it works.
Michael C
A: 

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