2013-02-11 65 views
0

我正在進行調查,如顯示問題和答案的項目。我使用標籤和按鈕在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,則所有0123void前檢查的radiobuttons也被禁用。我如何單獨處理單節的單選按鈕選擇。

回答

0

將單擊的indexPath存儲在字典中,並根據選定的值管理單元格顯示。根據存儲的值,通過在cellForRowAtIndexPath中創建它們時將它們設置爲選中。像:

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

編輯:selectedRows是包含了所選行的數據字典,還更新了它didSelectRowAtIndexPath,或點擊,但(如果你是這樣做的話)。

+0

對不起,我沒有得到你。我該在哪寫這篇文章?選擇什麼? – Honey 2013-02-11 07:42:44

+0

檢查編輯,並將其寫入'cellForRowAtIndexPath'中,正如我所提到的 – rptwsthi 2013-02-11 08:42:50