2012-05-24 60 views
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; 


    } 

} 

請幫忙。

回答

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 
    } 
} 
+0

不,它不幫我得到任何細胞的高度。它返回0.不管怎樣,謝謝。 – hgpl

相關問題