ios - 单击重置按钮后,UIbutton 不会重置为默认值
问题描述
我有一个产品过滤器列表,我想在单击“重置”按钮后将所有数据重置为默认值(无需单击)。
但现在我的情况是在我点击重置按钮后,选定的项目不会相应地重置,它会更改为其他项目。您可以参考以下截图:-
这是我的部分相关代码,请帮助并感谢您的建议:-
过滤器项目单元格.m
- (void)setUpUI
{
_contentButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_contentButton setTitleColor: ThemeBlueColor
forState:UIControlStateNormal];
[_contentButton addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_contentButton];
}
- (void)checkButtonTapped:(id)sender
{
if ([sender isSelected]) {
!_attributeItemClick ? : _attributeItemClick();
[sender setSelected: NO];
[_contentButton setImage:nil forState:0];
[_contentButton setTitleColor:ThemeBlueColor forState:UIControlStateNormal];
} else {
!_attributeItemClick ? : _attributeItemClick();
[sender setSelected: YES];
[_contentButton setImage:[UIImage imageNamed:@"isSelectYes"] forState:0];
[_contentButton setTitleColor:ThemeRedColor forState:UIControlStateNormal];
}
}
Filter_ViewController.m
- (void)setUpBottomButton
{
CGFloat buttonW = FiltrateViewScreenW/2;
CGFloat buttonH = 40;
CGFloat buttonY = ScreenH -10 - buttonH;
NSArray *titles = @[@"Reset",@"OK"];
for (NSInteger i = 0; i < titles.count; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:titles[i] forState:UIControlStateNormal];
button.tag = i;
CGFloat buttonX = i*buttonW;
button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
button.titleLabel.font = PFR16Font;
if (i == 0)
{
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"Button2-1"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(bottomButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}
else if (i == 1)
{
[button setBackgroundImage:[UIImage imageNamed:@"Button1-1"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(bottomButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}
else
{
}
[_filtrateConView addSubview:button];
}
}
- (void)bottomButtonClick:(UIButton *)button
{
if (button.tag == 0) //Reset Button
{
//CURRENT CODE TO RELOAD COLLECTION VIEW DATA
[self.collectionView reloadData];
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
FilterItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:FilterItemCellID forIndexPath:indexPath];
cell.attribute_name=[[self.filterItem[indexPath.section] valueForKey:@"attribute_name"]objectAtIndex:indexPath.row];
return cell;
}
解决方案
对于重置功能,您需要更新所有数据,就像您可能将勾选的数据存储在数组中一样。
您需要刷新用于在 collectionView 中显示的数组。然后你需要在你的 collectionView 上调用 reload。
在您的函数中,我只能看到重新加载函数调用,而不是任何用于刷新您可能用于在集合视图中显示的数组中的数据的函数。
我希望这可以帮助你。如果您需要其他任何东西,请告诉我。
推荐阅读
- rails-activestorage - 如何缩短 Rails Active Storage url 链接太长
- java - 如何遍历列表列表以操作内部列表的内容?
- sql - 如何在 Snowflake 中重命名表的多个列?
- java - Apache Commons CSVParser - 如何将列拆分为单独的键?
- python - 监控估算器进度和 sklearn 包装器
- html - 用 v-html 绑定图片
- java - 如何更改 Apache Tomcat 9 的 Java 版本
- javascript - 角度服务设置中的值未定义
- php - 如何通过 id laravel 过滤 json api 响应
- jenkins - jenkinspipeline groovy.lang.MissingMethodException:没有方法签名