gpt4 book ai didi

ios - 调用 viewDidLoad 时,单元格数据不会显示在 TableView 中

转载 作者:行者123 更新时间:2023-11-29 13:10:01 24 4
gpt4 key购买 nike

当我在 viewDidLoad 上加载 UITabelView 时,它不会在单元格中显示正确的数据,但是当我滚动 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;
}

最佳答案

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

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

关于ios - 调用 viewDidLoad 时,单元格数据不会显示在 TableView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17611396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com