【问题标题】:Select all check boxes at a time by clicking one UIButton通过单击一个 UIButton 一次选中所有复选框
【发布时间】:2015-06-15 18:24:39
【问题描述】:

谢谢大家。但我需要告诉你全部情况。我有一个集合视图单元格。在里面我添加了复选框。并且“全选”按钮位于集合视图之外。因此,如果我单击“全选”按钮,它应该选中集合视图单元格内的所有复选框。我在集合 viewcell 中添加了一个 customuibutton。 CustomUIButton *checkboxButton=[CustomUIButton buttonWithType:UIButtonTypeRoundedRect]; [checkboxButton setTaggy:indexPath.row]; //[checkboxButton setTag:CHECKBOX_BUTON_ON_CELL+indexPath.row+indexPath.section]; // [checkboxButton setCollectionIdentifier:collectionView.tag]; [复选框按钮添加目标:自我 动作:@selector(checkBoxAction:) forControlEvents:UIControlEventTouchUpInside]; checkboxButton.frame = CGRectMake(168.0, 3.0, 20.0, 20.0); [checkboxButton setSection:indexPath.section];

    [checkboxButton setHighlighted:NO];
    if (clsPartcipant.isSmallChanged)
    {
        [checkboxButton setBackgroundImage:[UIImage imageNamed:@"selected1.png"] forState:UIControlStateNormal];
    }
    else
    {
        [checkboxButton setBackgroundImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
    }
    [checkboxButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
    [cell.contentView addSubview:checkboxButton];

    });

return cell;

我调用了一个类似 checkBoxAction 的函数。 -(void)checkBoxAction:(id)sender{ CustomUIButton *button=sender; BOOL 标志 = 否;

if (button.selected)
{
    [button setSelected:NO];
    [button setBackgroundImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
    flag=NO;

}
else
{
    [button setSelected:YES];
    [button setBackgroundImage:[UIImage imageNamed:@"selected1.png"] forState:UIControlStateNormal];
    flag=YES;
}

if (button.collectionIdentifier == ClsRmPrctPresntCollectionView)
{
    [self ModifyDataArray:button.taggy flag:flag identifier:ClsRmPrctPresntCollectionView change:@"SmallButton" toDate:nil section:button.section];
    [self modifyCollectionArray:PRESENT processingData:self.presentDataArray identifier:ClsRmPrctPresntCollectionView index:button.taggy section:button.section];
}

} 现在我无法弄清楚我需要在 selectAll 按钮操作中添加什么。请在这方面帮助我

【问题讨论】:

  • 我的回答对你来说几乎是复制粘贴。但请务必阅读其评论。如果它适合你,则标记为正确

标签: ios ios7 ios5 uibutton


【解决方案1】:

我是用手机输入的,请原谅缺少代码。无论如何,我会足够清楚。

首先,您要将您的按钮链接为出口,所有这些,并给它们一个标签

在情节提要或代码中(在viewDidLoad 中),为selected 状态设置“选中”图像,为not selected 状态设置“未选中”图像。

现在您的按钮将知道与它们交互时要显示的内容。

对于每个按钮,您可能已经有一个 IBAction,这很好。您的 SelectAll 按钮也需要自己的按钮。

在所有按钮中,您将拥有

UIButton *bt = (UIButton*)sender;
[bt setSelected:!bt.isSelected];

现在我必须一到电脑前就检查一下,但基本上将按钮的选定属性设置为与其当前状态相反的状态。

对于你的全选,你可以使用这个

BOOL trigger;
    UIButton *bt = (UIButton*)sender;
    If(bt.isSelected == YES){
         trigger = NO;
}else{
         trigger = YES
}

for (int i = 0; i < 9 ; i++){ // note that 9 is the number of buttons

UIButton *bt = (UIButton*)[self.view viewWithTag:i]
[bt setSelected:trigger];
}

你应该已经准备好了:)

再次请原谅我手机上有任何荒谬的拼写错误或法语自动更正,我花了 20 分钟才写下来:D

【讨论】:

  • 虽然我想提一下,您应该能够通过研究提出解决方案。最后,您可以像其他答案所建议的那样对您的操作中的所有按钮进行硬编码,它也可以工作。我给你一个更优化的方法(我相信),但如果你不自学,这不会让你走得太远。
【解决方案2】:

首先,如果您使用UITabelView 来显示这些复选框,那就可以了,重新加载它,或者获取所有单元格然后重新加载每个单元格。
那么如果UIView上方有10个复选框,则可以使用NSMutableArray包含10个复选框,如:

for(UIButton * button in mArray){
 // do select or deselect action
}

【讨论】:

    猜你喜欢
    • 2015-05-12
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    相关资源
    最近更新 更多