Altura de la celda personalizada después de init en UITableView

I have a table with custom cell. These cells have different heights, and I know how to calculate the height. The problem is that the heightForRowAtIndexPath method is called before of method cellForRowAtIndexPath, so I can not pass the CGFloat of return.

Is there a way to invoke this method after?

Muchas gracias


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";

CustomCellFavorites *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[CustomCellFavorites alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    cell.topSuperiore = self;



NSString *string01 = ((Traduzioni *)[self.arrayTraduzioni objectAtIndex:indexPath.row]).testoOriginale;
NSString *string02 = ((Traduzioni *)[self.arrayTraduzioni objectAtIndex:indexPath.row]).testoTradotto;

[cell loadItem:string01 :string02];

return cell;

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCellFavorites *cell = (CustomCellFavorites*)[self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.heightCell;


custom cell method

-(void)loadItem:(NSString *)text01:(NSString *)text02 {
textView01.text = text01;
CGRect rect = textView01.frame;
rect.size.height = textView01.contentSize.height;
textView01.frame = rect;
[tv01Bg setFrame:CGRectMake(32, 0, 288, (textView01.contentSize.height)+20)];
textView02.text = text02;

CGRect rect2 = textView02.frame;
rect2.size.height = textView02.contentSize.height;
textView02.frame = rect2;
[tv02Bg setFrame:CGRectMake(0, (textView01.contentSize.height)+20, 320, (textView02.contentSize.height)+20)];
heightCell = (rect2.origin.y)+(rect2.size.height)+15.5;

preguntado el 08 de noviembre de 11 a las 15:11

1 Respuestas

No, there is no way to invoke this method after. The way I have handled this is to basically get the actual row & depending on the type of row, decide the height. Some brief code follows -

- (CGFloat)tableView:(UITableView *)tableView 
heightForRowAtIndexPath:(NSIndexPath *)indexPath
    NSObject *res     = [self.resultSet objectAtIndex:[indexPath row]];

    if([res isKindOfClass:[someX class]])
        return 110;
    else if([res isKindOfClass:[someY class]])
        return 120;
    else if([res isKindOfClass:[someZ class]])
        return 230;
        return 175.0;

respondido 08 nov., 11:19

Thanks, but the height can varied 100% and I can't create all the various possibilities. - Vinos

can you tell something more about the type of data you are handling? & the reason you need so much variation in height? - Srikar Appalaraju

Of course, I have two TextView in the cell, they vary based on what the user wrote. The sum of the heights and spaces are included in this heightCell Float ` - (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CustomCellFavorites *cell = (CustomCellFavorites)[self tableView:tableView cellForRowAtIndexPath:indexPath]; return cell.heightCell; }in the custom cell heightCell = (rect2.origin.y)+(rect2.size.height)+15.5;` - Vinos

then if you knew how many textViews are present then couldn't you figure out the total height then ? or couldn't you use cellID to figure out the type of cell & then custom code the height? - Srikar Appalaraju

since you are using custom cells, you know exactly how much height each type of cell ought to be. so if you extract the cellID in heightForRowAtIndexPath then based on that decide the height. - Srikar Appalaraju

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.