Josh Kahane
Josh Kahane

Reputation: 17169

Set Height Programmatically for a Single UITableViewCell?

I need to set the height for a single UITableViewCell in my UITableView programmatically. How can I do this?

I need this one cell to be 150 pixels high and all the others can stay at their default 44 pixels in height.

Thanks.

Upvotes: 11

Views: 24146

Answers (4)

Nick89
Nick89

Reputation: 2998

SWIFT ANSWER

Specify which section and row you want to change (remember the count starts at 0, not 1) in the heightForRowAtIndexPath method.

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
    if indexPath.section == 0 && indexPath.row == 0{
        return 150
    }
    return 44.0
}

Upvotes: 6

Alexander
Alexander

Reputation: 8147

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

You can set the value for the cell at a specific index path with this method, and a default value for the other cells.

Upvotes: 2

Louie
Louie

Reputation: 5940

You will have to know, or figure out what cell index you want to make the taller one. Lets say its your first cell.

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

    if (indexPath.row == 0) { //change 0 to whatever cell index you want taller
        return 150;
    }
    else {
        return 44;
    }   
}

Upvotes: 7

Max
Max

Reputation: 989

There is a delegate function for the UITableViewCell height.

Here you specify the indexPath of that particular cell and return your height for it

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == yourSection && indexPath.row == yourRow) {
        return 150.0;
    }
    // "Else"
    return someDefaultHeight;
}

Upvotes: 47

Related Questions