当我选中所有复选框时,所有复选框都被选中但是当我取消选中其中任何一个时我再次点击选择所有选项除了取消选中选项全部取消选中并选中取消选中。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath1
{
CFCell *cell;
if(indexPath1.row == 0)
{
for (int counter =0 ; counter < [self.dataTableView numberOfRowsInSection:0];counter++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:counter inSection:0];
cell = (CFEquipmentCell *)[self.dataTableView cellForRowAtIndexPath:indexPath];
if(selectedChecks[indexPath.row]==[NSNumber numberWithInt:1])
{
selectedChecks[indexPath.row] = [NSNumber numberWithInt:0];
cell.Checkbox.imageView.image = [UIImage imageNamed:@"unselected.png"];
}
else
{
selectedChecks[indexPath.row] = [NSNumber numberWithInt:1];
cell.Checkbox.imageView.image = [UIImage imageNamed:@"selected.png"];
}
}
}
else
{
cell = (CFCell *)[self.dataTableView cellForRowAtIndexPath:indexPath1];
if(selectedChecks[indexPath1.row]==[NSNumber numberWithInt:1])
{
selectedChecks[0] = [NSNumber numberWithInt:0];
selectedChecks[indexPath1.row] = [NSNumber numberWithInt:0];
cell.Checkbox.imageView.image = [UIImage imageNamed:@"unselected.png"];
}
else
{
selectedChecks[indexPath1.row] = [NSNumber numberWithInt:1];
cell.Checkbox.imageView.image = [UIImage imageNamed:@"selected.png"];
}
}
请在代码中解决我用于完美工作的问题。 1)我需要在我点击选择所有选项时选择全部复选框。 2)当我取消选中其中任何一个时,选择所有oprtion alos取消选中。 3)再次选择所有选项时选中所有复选框。
答案 0 :(得分:2)
请制作以下步骤:
创建等于行数的NSMutableArray
示例:No Of Rows = 10
NSMutableArray *checkmarkArray; //GLOBAL DECLARATION
- (void)viewDidLoad {
checkmarkArray=[[NSMutableArray alloc]init];
for (int i=0; i<[tableData count]; i++) // tableData is Number of Rows count
{
[checkmarkArray addObject:@"NO"];
}
}
第二步cellForRowAtIndexPath更改
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([checkmarkArray[indexPath.row] isEqualToString:@"NO"])
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"untick.png"]];
[cell.accessoryView setFrame:CGRectMake(0, 0, 15, 15)];
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick.png"]];
[cell.accessoryView setFrame:CGRectMake(0, 0, 15, 15)];
}
}
第三步选择所有按钮
-(IBAction)selectall_btnclick:(id)sender
{
for (int i=0; i<[tableData count]; i++)
{
[checkmarkArray replaceObjectAtIndex:i withObject:@"YES"];
}
[tableView reloadData];
}
`
第三步取消全选按钮
-(IBAction)deselectall_btnclick:(id)sender
{
for (int i=0; i<[tableData count]; i++)
{
[checkmarkArray replaceObjectAtIndex:i withObject:@"NO"];
}
[tableView reloadData];
}
下一步
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([checkmarkArray [indexPath.row] isEqualToString:@"YES"])
{
[checkmarkArray replaceObjectAtIndex:indexPath.row withObject:@"NO"];
}
else
{
[checkmarkArray replaceObjectAtIndex:indexPath.row withObject:@"YES"];
}
[tableView reloadData];}
这必须适用于您的概念。让我知道您需要任何澄清。
答案 1 :(得分:1)
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
//give your uncheck code here
}
在上面的委托方法
中提供取消选中的代码