参考方法中的一节

时间:2013-02-11 07:20:28

标签: cocoa-touch uitableview

我正在进行类似项目的调查,其中会显示问题和答案。我正在tableView中使用标签和按钮显示它们。但是现在我没有得到如何处理所选的每个答案。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 NSMutableArray *sectionItems=[self.finalarray objectAtIndex:indexPath.section];
    int n=[sectionItems count];

    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. numberOfLines=0;
    lblQuestion.frame = CGRectMake(10,15, size1.width, size1.height);

 UIButton *btTemp1 = [[UIButton alloc]initWithFrame:CGRectMake(10, lblQuestion.frame.origin.y+lblQuestion.frame.size.height,10, 15)];
        [btTemp1 addTarget:self action:@selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        btTemp1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [btTemp1 setImage:[UIImage imageNamed:@"radio_button_off.png"] forState:UIControlStateNormal];
        [btTemp1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btTemp1.titleLabel.font =[UIFont systemFontOfSize:14.f];
        [hlcell.contentView addSubview:btTemp1];



        NSLog(@"%@",[[sectionItems  objectAtIndex:1] objectForKey:@"Description"]);
        NSString *option1 = [NSString stringWithFormat:@"%@",[[sectionItems  objectAtIndex:1] objectForKey:@"Description"]];
        CGSize constraint2 = CGSizeMake(320, 2000.0f);
        CGSize size2 = [option1 sizeWithFont: [UIFont fontWithName:@"Helvetica-Bold" size:14] constrainedToSize:constraint2 lineBreakMode:NSLineBreakByWordWrapping];
        lblOption1 = (UILabel *)[hlcell viewWithTag:1];
        lblOption1.text = [NSString stringWithFormat:@"%@",option1];
        [lblOption1 setNumberOfLines:0];
        lblOption1.frame = CGRectMake(30,lblQuestion.frame.origin.y+lblQuestion.frame.size.height, size2.width, size2.height);
        [hlcell.contentView addSubview:lblanswers];

        UIButton *btTemp2 = [[UIButton alloc]initWithFrame:CGRectMake(10, lblOption1.frame.origin.y+lblOption1.frame.size.height,10, 15)];
        [btTemp2 addTarget:self action:@selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        btTemp2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [btTemp2 setImage:[UIImage imageNamed:@"radio_button_off.png"] forState:UIControlStateNormal];
        [btTemp2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btTemp2.titleLabel.font =[UIFont systemFontOfSize:14.f];
        [hlcell.contentView addSubview:btTemp2];



        NSString *option2 = [NSString stringWithFormat:@"%@",[[sectionItems  objectAtIndex:2] objectForKey:@"Description"]];
        CGSize constraint3 = CGSizeMake(320, 2000.0f);
        CGSize size3 = [option2 sizeWithFont: [UIFont fontWithName:@"Helvetica-Bold" size:14] constrainedToSize:constraint3 lineBreakMode:NSLineBreakByWordWrapping];
        lblOption2 = (UILabel *)[hlcell viewWithTag:2];
        lblOption2.text = [NSString stringWithFormat:@"%@",option2];
        [lblOption2 setNumberOfLines:0];
        lblOption2.frame = CGRectMake(30,lblOption1.frame.origin.y+lblOption1.frame.size.height, size3.width, size3.height);
        [hlcell.contentView addSubview:lblOption2];

return hlcell;
}

我有10个问题,如何在每个部分中选择并存储答案。 我无法引用indexpath。 radioButtonClicked方法中的部分。如果我在一个部分中选择radiobutton,则会启用它,而其他部分则被禁用。但是,如果我在其他部分中检查radiobutton,则所有radiobuttons预先检查的也会被禁用。如何单独处理部分的单选按钮选择。

1 个答案:

答案 0 :(得分:0)

将词典中的indexPath存储在词典中,并根据所选值管理单元格显示。根据存储的值,在cellForRowAtIndexPath中创建它们时选择它们。像:

if([[selectedRows valueForKey:stringRepresentationOfIndexPath] boolValue] == YES)
  [btTemp1 setSelected:YES];

编辑: selectedRows是包含所选行数据的字典,在didSelectRowAtIndexPath中更新,或者点击(如果您这样做)