C.Johns
C.Johns

Reputation: 10245

setting uitableviewcell height for custom cell

I was looking to set up my cell heights dynamically based off of the value that you set the custom cell in Interface Builder. This did not work out so I have moved on and found this method in Apples docs that can be used to set the cell height - tableView:heightForRowAtIndexPath:

However I am not sure if I am using it correctly as none of my tableviewcells are adjusting their height they all stay the same standard size. This is the code I am using here.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Set cell height
    [self tableView:tableView heightForRowAtIndexPath:indexPath];

    // Configure the cell using custom cell
    [[NSBundle mainBundle] loadNibNamed:@"CustomcellA" owner:self options:nil];
    cell = customcellA; // customcellA getters and setters are set up in .h

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

any help would be greatly appreciated

Upvotes: 1

Views: 4535

Answers (2)

vikingosegundo
vikingosegundo

Reputation: 52237

lets say the third cell is twice as height:

- (CGFloat)   tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 2)
        return 88;
    return 44;
}

This method will be called by the tableView.

You shouldn't do this:

[self tableView:tableView heightForRowAtIndexPath:indexPath];

It is up to you, how you ensure, that the right cell's indexPath is identified for other sizes.

General Rule:
If you implement the method of a delegate protocol, you will never call it yourself. Only the object of the class, that has a delegate conforming to a protocol, will call the delegate methods.

Upvotes: 7

rooster117
rooster117

Reputation: 5552

heightForRowAtIndexPath will be called automatically and you need to return the custom height that you need. So rather than return 44 you will need to return a dynamic height based on your data or whatever you are basing your height on.

Upvotes: 0

Related Questions