0

我需要在每个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

4

1 回答 1

0

你试试这个

1 你给标签所有的复选框和行的单元格并在 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 "); 
                }
              }

    } 
    }
}

我希望这能解决你的问题

于 2013-02-13T08:29:24.923 回答