0

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

- (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;


    }

}

请帮忙。

4

1 回答 1

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
    }
}
于 2012-05-24T06:51:42.473 回答