在UITableViewCell中循环

时间:2013-02-09 11:13:28

标签: iphone uitableview xcode4.5

我正在做一个测验项目,其中问题的数量和问题的选项数量是动态的(取自服务网址)。问题的最多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;

    }





}

1 个答案:

答案 0 :(得分:0)

你做错了。将工作细分为两部分:

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

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

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

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

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

您可以从此处下载示例代码:http://www.saplogix.net/QA/sample.zip