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