UIButton 点击​​发件人总是转到选定的选项

Posted

技术标签:

【中文标题】UIButton 点击​​发件人总是转到选定的选项【英文标题】:UIButton click sender always go to selected option 【发布时间】:2018-07-26 00:18:20 【问题描述】:

我有一个 UIButton 点击​​发送者如下:-

-(IBAction)clipButtonClick:(UIButton *)sender

    sender.selected  = ! sender.selected;

    if (sender.selected)
    
        [btnClipCategory setImage:[UIImage imageNamed:@"imgClip_HL"] forState:UIControlStateSelected];

        NSMutableArray* indexArray = [NSMutableArray array];
        for (NSInteger i = 0; i < self.arySubCategory.count; i++)
        
            [aryCheckCategoryList replaceObjectAtIndex:i withObject:@"YES"];
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            [indexArray addObject:indexPath];

            dispatch_async(dispatch_get_main_queue(), ^
                [self.rightTableView reloadData];
            );

        

    
    else
    
        [btnClipCategory setImage:[UIImage imageNamed:@"imgClip"] forState:UIControlStateNormal];

        NSMutableArray* indexArray = [NSMutableArray array];
        for (NSInteger i = 0; i < self.arySubCategory.count; i++)
        
            [aryCheckCategoryList replaceObjectAtIndex:i withObject:@"NO"];
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            [indexArray addObject:indexPath];



            dispatch_async(dispatch_get_main_queue(), ^
                [self.rightTableView reloadData];

            );

        

    

当我点击按钮时,发件人总是会点击选定的选项。

如果我将发件人更改为示例代码,如下所示,我的应用程序将按预期正常工作。任何的想法?请帮忙。谢谢。

-(IBAction)clipButtonClick:(UIButton *)sender

    sender.selected  = ! sender.selected;

    if (sender.selected)
    
        [btnClipCategory setImage:[UIImage imageNamed:@"imgClip_HL"] forState:UIControlStateSelected];
        NSLog(@"Selected. Will Hit here");

    
    else
    
        [btnClipCategory setImage:[UIImage imageNamed:@"imgClip"] forState:UIControlStateNormal];
        NSLog(@"Non Selected. Will Hit here");

    

已编辑 我将按钮放在 willDisplayHeaderView 下。

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section

    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;

    btnClipCategory = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnClipCategory setFrame:CGRectMake(header.frame.size.width - 45, header.frame.size.height * 0.2, ScreenW * 0.1, ScreenW * 0.1)];

    [btnClipCategory addTarget:self action:@selector(clipButtonClick:) forControlEvents:UIControlEventTouchUpInside];

    [header addSubview:btnClipCategory];


【问题讨论】:

sender 视图是否位于rightTableView 单元格内的某个位置? 嗨@EgorKomarov,我把它放在willDisplayHeaderView下。您可以参考上面的编辑代码 【参考方案1】:

当您调用[self.rightTableView reloadData] 时,表视图的所有内容(包括节标题)都会重新加载,并且会为每个可见的节标题调用tableView:willDisplayHeaderView:forSection:。因此,每次单击操作触发时,您的 bntClipCategory 都会使用默认值 isSelected 重新创建。 您无需在工作代码示例中重新加载 rightTableView,这就是它起作用的原因。

你应该重新考虑你的方法。其中一个选项是为数组中的每个部分保存isSelected 按钮的状态,并在将其添加到标题视图之前相应地更新btnClipCategory.isSelected

【讨论】:

嗨@Egor Komarov,感谢您的解释,介意在这里分享一些示例代码吗?感谢您的帮助!

以上是关于UIButton 点击​​发件人总是转到选定的选项的主要内容,如果未能解决你的问题,请参考以下文章

从 UIPickerView 的选定行设置 UIButton 的标题

从选定的不同类设置 UIButton

如何更改以编程方式创建的一组 UIButton 的标题颜色?

如何从数组中过滤多个选定值并将其从 uibutton 加载到其他 uitableview

类型“UIButton”没有成员“发件人”

在发件人中使用带有 UIButton 的标签属性并尝试强制转换 id -> UIButton