1

当我加载它时UITabelViewviewDidLoad它不会在单元格中显示正确的数据,但是当我滚动 tabelview 时,它会设置正确的数据。我的代码有什么问题?

这是我使用的代码。

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

1 回答 1

1

您不能设置单元格的框架-tableView:cellForRowAtIndexPath:。这是由表视图自动处理的。

要告诉表格视图单元格所需的高度,请使用-tableView:heightForRowAtIndexPath:.

于 2013-07-12T10:27:52.250 回答