2013-07-12 21 views
1

當我在viewDidLoad上加載UITabelView時,它不會在單元格中顯示正確的數據,但是當我滾動tabelview時,它會設置正確的數據。我的代碼有什麼問題?當調用viewDidLoad時,單元格數據不會顯示在UITableView中

這裏我使用的代碼。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    CustomCell *cell = [_AnswerKeyTable dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 

     for(id currentObject in topLevelObject) 
     { 
      if([currentObject isKindOfClass:[UITableViewCell class]]) 
      { 
       cell = (CustomCell*)currentObject; 
      } 
     }   
    } 

    ModelInfo *info = [mArray objectAtIndex:indexPath.row]; 
    CGSize constraint = CGSizeMake(212, 20000.0f); 

    NSString *temp=[NSString stringWithFormat:@"%@ (%@)",info.your_answer,info.answer_status]; 

    CGSize question_size = [info.question sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

    CGSize your_answer_size = [temp sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

    CGSize correct_answer_size = [info.correct_answer sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

    NSString * ans_string; 
    if([info.answer_status isEqualToString:@"Correct answered"]) 
    { 
     ans_string = [NSString stringWithFormat:@"%@(<font color=\"#7EBA00\"><b>%@</b></font>)",info.your_answer,info.answer_status]; 
     cell.lblCorrectAnswer.hidden = YES; 
     cell.lblC_Ans.hidden = YES; 

    }else if([info.answer_status isEqualToString:@"Not answered"]){ 
     ans_string = [NSString stringWithFormat:@"<font color=\"#FF0000\"><b>%@</b></font>)",info.answer_status]; 
     cell.lblCorrectAnswer.hidden = NO; 
     cell.lblC_Ans.hidden = NO; 
     cell.lblCorrectAnswer.text =info.correct_answer; 
     [cell.lblCorrectAnswer setFrame:CGRectMake(103,your_answer_size.height+question_size.height+9,212,correct_answer_size.height)]; 
     [cell.lblC_Ans setFrame:CGRectMake(cell.lblC_Ans.frame.origin.x,your_answer_size.height+question_size.height+9,cell.lblC_Ans.frame.size.width,21)]; 

    }else{ 
     ans_string = [NSString stringWithFormat:@"%@(<font color=\"#FF0000\"><b>%@</b></font>)",info.your_answer,info.answer_status]; 
     cell.lblCorrectAnswer.hidden = NO; 
     cell.lblC_Ans.hidden = NO; 
     cell.lblCorrectAnswer.text =info.correct_answer; 
     [cell.lblCorrectAnswer setFrame:CGRectMake(103,your_answer_size.height+question_size.height+9,212,correct_answer_size.height)]; 
     [cell.lblC_Ans setFrame:CGRectMake(cell.lblC_Ans.frame.origin.x,your_answer_size.height+question_size.height+9,cell.lblC_Ans.frame.size.width,21)]; 
    } 

    cell.lblQuestion.text = info.question; 
    cell.lblYourAnswer.text = ans_string; 
    cell.lblYourAnswer.attributedText=[OHASBasicHTMLParser attributedStringByProcessingMarkupInAttributedString:cell.lblYourAnswer.attributedText]; 
    cell.lblQue.text = [NSString stringWithFormat:@"Question %d",indexPath.row+1]; 
    [cell.lblQuestion setFrame:CGRectMake(103, 3,212,question_size.height)]; 
    [cell.lblYourAnswer setFrame:CGRectMake(103,cell.lblQuestion.frame.size.height+6,212,your_answer_size.height)]; 
    [cell.lblAns setFrame:CGRectMake(cell.lblAns.frame.origin.x,cell.lblQuestion.frame.size.height+6,cell.lblAns.frame.size.width,21)]; 
    [cell.view setFrame:CGRectMake(5,1,310,cell.lblQuestion.frame.size.height+cell.lblYourAnswer.frame.size.height+cell.lblCorrectAnswer.frame.size.height)]; 
    [cell setFrame:CGRectMake(0,0,320,cell.view.frame.size.height)]; 
    [cell setFrame:CGRectMake(0,0,320,cell.frame.size.height+10)]; 
    return cell; 
} 
+0

有什麼問題可以分享你的屏幕,然後我們可以理解? – Anjaneyulu

+0

[objectname reloaddata];加載tableview @Hiren – Anjaneyulu

+0

你使用的是任何UITextView?如果是這樣,請嘗試使用UILabel而不是numberoflines = 0並檢查它是否仍在發生。 – ratul

回答

1

您無法設置單元的框架-tableView:cellForRowAtIndexPath:。這是由表視圖自動處理的。

要告訴表格視圖單元格所需的高度,請使用-tableView:heightForRowAtIndexPath:

+0

謝謝傑弗瑞,現在我也處理tabelview高度和它的完美,但問題是第一次顯示數據...當我向下滾動其可見性...每個控件設置正確,但第一次顯示dnt。 – Hiren

相關問題