Dynamic height UITableViewCell and overlapping content

Jul 30, 2010   #cocoa  #ios 

If you want a UITableView that has different cell sizes depending on whether the cell is selected or not, you might use something like this in your UITableViewDelegate:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = 40;    
    if(indexPath.row == selectedRow) { //selectedRow is set in - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        height += 100;
    }
    return height;
}

If you are using custom cells with content larger than the height of an unselected row, the cell content may overlap with other cells. To prevent this, just set cell.clipsToBounds = YES.