1

When I load UITabelView on viewDidLoad it does not show proper data in cells, but when I scroll the tabelview it sets proper data. What's wrong in my code?

Here the code I use.

-(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;
}
3
  • what is the problem can u pls share ur screen then we can understand ?
    – Anjaneyulu
    Jul 12, 2013 at 9:16
  • [objectname reloaddata]; one's loading the tableview @Hiren
    – Anjaneyulu
    Jul 12, 2013 at 9:17
  • Are you using any UITextView? If so, try UILabel instead with numberoflines=0 and check it is still happening.
    – ratul
    Jul 12, 2013 at 9:19

1 Answer 1

1

You can't set the frame of a cell -tableView:cellForRowAtIndexPath:. That is handled automatically by the table view.

To tell the table view the height needed for a cell, use -tableView:heightForRowAtIndexPath:.

1
  • Thanks Jeffery, now I m also handle the tabelview height and its perfect but problem is first time data showing ...when i scroll down its visible quickly...every controls set properly but dnt showing in first time.
    – Hiren
    Jul 17, 2013 at 7:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.