gpt4 book ai didi

ios - UIButton 单击发件人总是转到选定的选项

转载 作者:行者123 更新时间:2023-11-28 23:54:29 25 4
gpt4 key购买 nike

我有一个 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];

}

最佳答案

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

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

关于ios - UIButton 单击发件人总是转到选定的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51529350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com