UITableViewCell中的多个复选框

时间:2013-02-13 06:03:07

标签: iphone uitableview checkbox xcode4.5

我需要在每个UITableViewCell中添加多个复选框。如果只有一个复选框,我知道如何做,我们检查状态。但是当我采用多个复选框时,复选框状态无法正确显示。当我点击两次时,我可以勾选复选框。

-(IBAction)checkboxClicked:(id)sender
{

    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.array 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;
    }
}

我哪里错了?如何处理UITableViewCell中的多个复选框?

1 个答案:

答案 0 :(得分:0)

你试试这个

1您为所有CheckBox和行单元格提供标签并在cellForRowAtIndexPath上查看  2,点击按钮,你尝试写这个代码

自定义类中的

进行按钮操作并调用传递视图标记的父方法

ArrayName = Array保存给定表的所有数据实际保存所有视图(在此数组中我们保存在单元格上显示的视图,换句话说它存储完整的单元格)

像这样的自定义类方法

- (IBAction)button_Action:(UIButton *)sender {
        [parentView Action:self.view.tag BtnTag:sender.tag];
}

在父类

 -(void)Remove:(int ) BtnTag  :btnTag
for (int i = 0; i<[ArrayName count]; i++)
{
    customCell *vca=[ArrayName objectAtIndex:i];   
    if(vca.view.tag == viewTag) // here check the cell where we tap or click
    {
         for (UIView* view in [self.view subviews]) {
           if([view isKindOfClass:[UIButton class]]){
                 UIButton *btn = (UIButton*)view; 
                     if(btn.tag == btnTag)
                 NSLog(@"You write this your own code what you want to do "); 
                }
              }

    } 
    }
}

我希望这能解决你的问题