Armanoide
Armanoide

Reputation: 1348

Probleme with height UITableViewCell

This is my story:

I have a problem with the size of my IUTableViewCell. When I add several cell, the cell auto resizing.

any answer will be appreciated :)

That my code to resize:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPathInCellTable:(NSIndexPath *)indexPath {
    
    CustomCell *cell = (CustomCell*) [self tableView:tableView
                         cellForRowAtIndexPath:indexPath];
    CGSize size;
    
    // SIZE HEIGHT TEXT
        size = [cell.color.text sizeWithAttributes:
                @{NSFontAttributeName:
                      [UIFont systemFontOfSize:12.0f]}];
    
      // SIZE HEIGHT FOR CELL
        CGRect frame = [cell frame];
        frame.size.height +=  size.height;
        [cell setFrame:frame];
    
      // SIZE HEIGHT IMG
        CGRect frame = [cell.img frame];
        frame.size.height = 69;
        frame.size.width = 69;
        [cell.img setFrame:frame];
        
    if (indexPath.row == 0) [self setHeightTableView:0];
    _tableHeightConstraint.constant += cell.frame.size.height;
    
    return cell.frame.size.height;
}

There some screenshot :

the first time i add a cell everything is fine

enter image description here

the same for the second cell everything is fine

enter image description here

And there the problem comes enter image description here

Upvotes: 1

Views: 77

Answers (2)

Basheer_CAD
Basheer_CAD

Reputation: 4919

You call CustomCell

 *cell = (CustomCell*) [self tableView:tableView
                         cellForRowAtIndexPath:indexPath];

to get the a cell, which is wrong, because there is no cell created yet (so its nil). tableView:heightForCell:atIndexPath: get called before the cell was created. The best solution would be to have a module abject to save the height needed for your cell or make some similar calculations

Upvotes: 1

CrimsonChris
CrimsonChris

Reputation: 4641

Use a prototype cell to get the sizing instead of using tableView:cellForRowAtIndexPath:

http://www.samrayner.com/posts/dynamic-tableview-cells/

Upvotes: 0

Related Questions