作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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/
我是一名优秀的程序员,十分优秀!