从下到上计算表格中的单元格

时间:2012-05-24 06:45:02

标签: iphone uitableview

我需要从下到上计算细胞的高度,因为我需要在我的第一个细胞中加入其他细胞的高度。如何计算最后的单元格高度,然后计算倒数第二个单元格...依此类推,最后计算第一个单元格高度。请帮忙。我在这里结构非常糟糕。我的代码是这样的...它从上到下给出了高度。

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

    if (indexPath.section==0 && currentQuestion.graphic){       
        UIImage *img= [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:currentQuestion.graphic ofType:nil]];
        self.currentQuestionImage=img;
        [img release];

        return (self.currentQuestionImage.size.height* [self zoomFactor])+10;
    }else if ((indexPath.section==0 && !currentQuestion.graphic) || (indexPath.section==1 && currentQuestion.graphic)){

        CGSize constSize = { self.view.frame.size.width, 20000.0f };
        CGSize textSize = [currentQuestion.text sizeWithFont:[ThemeSettings fontUsedInQuestionCell] constrainedToSize:constSize lineBreakMode:UILineBreakModeWordWrap];
        CGFloat height =  (textSize.height)+60.0;

        return height<45.0?45.0:height;  


        }else if([self answerGraphic: indexPath]){
        UIImage *img = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[self answerGraphic: indexPath] ofType:nil]];
        [self.answerImages replaceObjectAtIndex:indexPath.section withObject:img];
        int size = (img.size.height*0.50)+10;
        [img release];

        return size;

    }else {
        int answerIndex = currentQuestion.graphic?indexPath.section-2:indexPath.section-1;
        Answer *a=[currentQuestion.answers objectAtIndex:answerIndex];
        CGSize constSize = { 300.0f, 20000.0f };
        CGSize textSize = [a.text sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:constSize lineBreakMode:UILineBreakModeWordWrap];
        CGFloat height =  (textSize.height+textSize.height/3.0 )+10.0;
        return height<55.0?55.0:height;


    }

}

请帮忙。

1 个答案:

答案 0 :(得分:0)

您无法强制执行计算顺序。但是heightForRowAtIndexPath可以随时计算任何高度。所以,有些事情会起作用:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0)
    {
        CGFloat h = 0;
        for (int i = 1; i < [self tableView:tableView numberOfRowsInSection:indexPath.section]; i++)
            h += [self tableView:tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:indexPath.section]];
        return h;
    }
    else
    {
        return 44.; //or any other value for your other cells
    }
}