【问题标题】:UIbutton does not reset to default after click on Reset button单击重置按钮后,UIbutton 不会重置为默认值
【发布时间】: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


【解决方案1】:

对于重置功能,您需要更新所有数据,就像您可能将勾选的数据存储在数组中一样。

您需要刷新用于在 collectionView 中显示的数组。 然后你需要在你的 collectionView 上调用 reload。

在您的函数中,我只能看到 reload 函数调用,而不是任何用于刷新您可能用于在集合视图中显示的数组中的数据的函数。

我希望这对你有帮助。如果您需要其他任何东西,请告诉我。

【讨论】:

    猜你喜欢
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 2018-02-17
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多