【发布时间】:2018-10-13 16:54:20
【问题描述】:
我有一个产品过滤器列表,我想在点击重置按钮后将所有数据重置为默认值(无需点击)。
但现在我的情况是在我点击重置按钮后,选定的项目不会相应地重置,它会更改为其他项目。您可以参考以下截图:-
这是我的部分相关代码,请帮助并感谢您的建议:-
FilterItemCell.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;
}
【问题讨论】:
-
重置按钮的代码是什么?
-
其余代码的bottomButtonClick方法?
-
为项目代码添加collectionview单元格。
-
@JasveerSingh,如上添加和更新。
-
您需要将所有选定的项目保存在一个数组中,并且在 cellForRowAtIndex 中只需检查当前项目是否在选定的数组中,然后将项目设置为选中,否则将项目设置为未选中。当您要重置列表时,请从所选数组中删除所有项目并重新加载集合视图。希望这会有所帮助。
标签: ios objective-c uibutton reload