Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can we change the height of current row in UITableView?

A very simple question...

can we change the height of only current row(the row which is clicked) in UItableView?

Note: I dont want any affect on size of remaining rows.

like image 425
nitz19arg Avatar asked Jan 13 '23 09:01

nitz19arg


1 Answers

Yes you can. You need a variable that keeps the last selected row. For ex.:

@property (nonatomic, assign) NSInteger selectedRow;

... Then implement the method

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == self.selectedRow) {
        return 100.;
    }

    return 44.;
}

and then, update the method:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if(self.selectedRow == indexPath.row)
        self.selectedRow = -1;
    else
        self.selectedRow = indexPath.row;

    //The magic that will call height for row and animate the change in the height
    [tableView beginUpdates];
    [tableView endUpdates];
}

NOTE: Initialise your self.selectedRow with -1 value in the beginning as the default is 0.

like image 89
graver Avatar answered Jan 29 '23 11:01

graver