0

我正在做一个测验项目,其中问题的数量和问题的选项数量是动态的(取自服务 url)。一个问题最多 5 个选项(但可能会减少)。我在分段的 UITableView 中显示它,每个问题及其选项显示在一个部分中。我将对象放在 NSMutableArray 的一个部分中并每次循环选项。但我只得到最后一个选项。我不明白为什么会这样?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *hlCellID=@"hlCellID";

    UITableViewCell* hlcell=[tableView dequeueReusableCellWithIdentifier:hlCellID];

    if(hlcell == nil)
    {
        hlcell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault    reuseIdentifier:hlCellID]autorelease];

        hlcell.accessoryType = UITableViewCellAccessoryNone;
        hlcell.selectionStyle = UITableViewCellSelectionStyleNone;
        lblQuestion = [[UILabel alloc] initWithFrame:CGRectZero];
        lblQuestion.tag = 1;
        lblQuestion.backgroundColor = [UIColor clearColor];
        [lblQuestion setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14]];
        [lblQuestion setLineBreakMode:NSLineBreakByWordWrapping];
        [hlcell addSubview:lblQuestion];

        lblanswers = [[UILabel alloc] initWithFrame:CGRectZero];
        lblanswers.tag = 2;
        lblanswers.backgroundColor = [UIColor clearColor];
        [lblanswers setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
        [lblanswers setLineBreakMode:NSLineBreakByWordWrapping];
        [hlcell addSubview:lblanswers];

    }
    int section=indexPath.section;
    NSMutableArray *sectionItems=[self.finalarray objectAtIndex:indexPath.section];
    int n=[sectionItems count];

   hlcell.selectionStyle = UITableViewCellSelectionStyleNone;


    NSString *question = [NSString stringWithFormat:@"%@",[[sectionItems  objectAtIndex:indexPath.row] objectForKey:@"Question"]];
    CGSize constraint1 = CGSizeMake(320, 2000.0f);
    CGSize size1 = [question sizeWithFont: [UIFont fontWithName:@"Helvetica-Bold" size:14] constrainedToSize:constraint1 lineBreakMode:NSLineBreakByWordWrapping];
    lblQuestion = (UILabel *)[hlcell viewWithTag:1];
    lblQuestion.text = [NSString stringWithFormat:@"%@",question];
    [lblQuestion setNumberOfLines:0];
    lblQuestion.frame = CGRectMake(10,25, size1.width, size1.height);


    for(int j=1;j<n;j++)
    {

        NSLog(@"%@",[[sectionItems  objectAtIndex:j] objectForKey:@"Description"]);
        NSString *answer = [NSString stringWithFormat:@"%@",[[sectionItems  objectAtIndex:j] objectForKey:@"Description"]];
        CGSize constraint2 = CGSizeMake(320, 2000.0f);
        CGSize size2 = [answer sizeWithFont: [UIFont fontWithName:@"Helvetica-Bold" size:14] constrainedToSize:constraint2 lineBreakMode:NSLineBreakByWordWrapping];
        lblanswers = (UILabel *)[hlcell viewWithTag:2];
        lblanswers.text = [NSString stringWithFormat:@"%@",answer];
        [lblanswers setNumberOfLines:0];
        lblanswers.frame = CGRectMake(10,20*j, size2.width, size2.height);
        [hlcell.contentView addSubview:lblanswers];



    }

    return hlcell;

}

我不明白如何为 lblanswers 设置框架。

编辑:

-(IBAction)checkboxClicked:(id)sender
{
    NSMutableArray *arr1=[[NSMutableArray alloc]init];

    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)
                              [[sender superview] superview]];


    NSLog(@"The section  is %d",  indexPath.section);
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    NSMutableArray *cellSection = [self.finalarray objectAtIndex:indexPath.section];
    int tagID=[sender tag];

    int divnum=0;
    if(tagID<100)
        divnum=10;
    else
        divnum=100;
    int section=[sender tag]/divnum;
    section-=1;
    int itemId=[sender tag]%divnum;

    if(itemId==selectedrowforCheckBox)
    {

        if(self.isChecked ==NO)
        {
            self.isChecked =YES;
            [sender setImage:[UIImage imageNamed:@"checkbox_checked.png"] forState:UIControlStateNormal];

            NSNumber* xWrapped = [NSNumber numberWithInt:itemId];
            int m=[cellSection count]-1;
            NSString *questionId=[[cellSection objectAtIndex:m-1]objectForKey:@"QId"];

            NSMutableDictionary *hk=[[NSMutableDictionary alloc]init];
            [hk setObject:xWrapped forKey:@"Ans"];
            [hk setObject:questionId forKey:@"QId"];
            [selectedOptionandQIdArray addObject:hk];





        }
       else
        {
            self.isChecked =NO;
            [sender setImage:[UIImage imageNamed:@"checkbox_unchecked.png"]forState:UIControlStateNormal];

            NSNumber* xWrapped = [NSNumber numberWithInt:itemId];
            int m=[cellSection count]-1;
            NSString *questionId=[[cellSection objectAtIndex:m-1]objectForKey:@"QId"];

            for (int i=0;i<[selectedOptionandQIdArray count];i++)
            {

                NSString *qid=[[selectedOptionandQIdArray objectAtIndex:i]objectForKey:@"QId"];
                if(xWrapped==[[selectedOptionandQIdArray objectAtIndex:i]objectForKey:@"Ans"] && [questionId isEqualToString:qid])
                {

                    NSLog(@"nnd");
                    [selectedOptionandQIdArray removeObjectAtIndex:i];

                }




            }



        }

        selectedrowforCheckBox=itemId; 

    }
    else
    {

        if(self.isChecked ==NO)
        {
            self.isChecked =YES;
            [sender setImage:[UIImage imageNamed:@"checkbox_checked.png"] forState:UIControlStateNormal];

            NSNumber* xWrapped = [NSNumber numberWithInt:itemId];
            int m=[cellSection count]-1;
            NSString *questionId=[[cellSection objectAtIndex:m-1]objectForKey:@"QId"];

            NSMutableDictionary *hk=[[NSMutableDictionary alloc]init];
            [hk setObject:xWrapped forKey:@"Ans"];
            [hk setObject:questionId forKey:@"QId"];


            [selectedOptionandQIdArray addObject:hk];



        }
        else
        {
            self.isChecked =NO;
            [sender setImage:[UIImage imageNamed:@"checkbox_unchecked.png"]forState:UIControlStateNormal];


        }

        selectedrowforCheckBox=itemId;

    }





}
4

1 回答 1

0

你这样做是不对的。将工作分解为两部分:

在此之前创建一个字典,其中每个键是问题,关联对象是一个包含所有答案的数组。

  • 部分数= [字典计数]
  • 每个部分的行数 = [[dictionary valueForKey:[[dic allKeys] objectAtIndex:indexPath.section]] count]

    1. 问题 - 使用 UITextView 创建一个自定义视图并将其作为每个部分的标题返回,您可以根据问题通过计算和返回高度来调整高度。

    2. 答案 - 使用 UITextView 创建一个自定义单元格并根据部分和行设置文本。

这种策略不仅可以通过给定结构来简化代码,还可以指定用户选择的答案的检测,因为您可以只存储 indexPath 并检查问题的部分和用户选择的答案的行。

你可以从这里下载示例代码:http: //www.saplogix.net/QA/sample.zip

于 2013-02-09T12:06:12.177 回答