【问题标题】:Changing image of a button in UITableViewCell on clicking it.单击它时更改 UITableViewCell 中按钮的图像。
【发布时间】:2018-09-11 16:41:23
【问题描述】:

我正在开发一个应用程序,其中 UITableViewCellUIButton 的图像应该在 click 时发生变化,我已经在自定义单元格。现在,我可以更改按钮的图像,但当我向下滚动时,它也会更改其他几个按钮的图像(因为在滚动时调用cellForRowAtIndexPath:)。

这是代码。

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
        _cell = (ListTableViewCell *)[self.tblList dequeueReusableCellWithIdentifier:@"listTableViewCell"];
        if (_cell == nil) {
            _cell = [[ListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"listTableViewCell"];
        } else {
            _cell.imgIcon.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[_arrImages objectAtIndex:indexPath.row]]];
            _cell.lblList.text = [NSString stringWithFormat:@"%@",[_arrNames objectAtIndex:indexPath.row]];
            _cell.btnList.tag = indexPath.row;
            if (_cell.btnList.tag == indexPath.row) {
                [_cell.btnList addTarget:self action:@selector(btnPressedMethodCall:) forControlEvents:UIControlEventTouchUpInside];
            }
        }
        return _cell;
    }


- (void) btnPressedMethodCall:(UIButton*)sender  {
    if ([sender isSelected]) {
        [sender setImage:[UIImage imageNamed:@"red_image.png"] forState:UIControlStateSelected];
        [sender setSelected:NO];
    } else {
        [sender setImage:[UIImage imageNamed:@"black_image.png"] forState:UIControlStateNormal];
        [sender setSelected:YES];
    }
}

有人能告诉我如何解决这个问题吗?感谢任何帮助。

【问题讨论】:

  • 查看此链接Tableview button click
  • tableview 需要一个数据源来显示数据。更改按钮属性后,需要更新表格数据源,以便表格视图正常显示。
  • 请在您的操作上重新加载 uitableviewcell - btnPressedMethodCall
  • 最初设置两个状态图像,然后在按钮单击时调用[sender setSelected:NO/YES]。您还需要在 cellForRowAtIndexPath 中处理此问题,因为此方法会调用滚动。

标签: ios objective-c uitableview


【解决方案1】:

而不是在按钮单击事件中更改图像。在NSMutableArraycellForRow 方法检查NSMutableArray 中添加选定的button indexPath 包含当前indexPath。如果是,则更改 button 图像,否则设置正常图像,如下所示。

斯威夫特

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:TableViewCell = self.tblVW.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell

    cell.selectionStyle = .none

    cell.btn.tag = indexPath.row
    cell.btn.addTarget(self, action: #selector(btnTapped), for: .touchUpInside)

    if arrIndexPaths.contains(indexPath) {
        cell.btn.setImage(YOUR_BUTTON_SELECTED_IMAGE, for: .normal)
    }
    else {
        cell.btn.setImage(YOUR_BUTTON_DEFAULT_IMAGE, for: .normal)
    }
    cell.layoutSubviews()
    return cell;
}

@IBAction func btnTapped(_ sender: UIButton) {
    let selectedIndexPath = NSIndexPath.init(row: sender.tag, section: 0)
    arrIndexPaths.add(selectedIndexPath)
    self.tblVW.reloadData()
}

如果您只想重新加载单行而不是将self.tblVW.reloadData() 替换为self.tblVW.reloadRows(at: [selectedIndexPath as IndexPath], with: UITableViewRowAnimation.none)

目标 C

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"TableViewCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil] objectAtIndex:0];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.btn.tag = indexPath.row
    [cell.btn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];

    if ([arrIndexPaths containsObject: indexPath]) {
        [cell.btn setImage:YOUR_BUTTON_SELECTED_IMAGE forState:UIControlStateNormal];
    }
    else {
        [cell.btn setImage:YOUR_BUTTON_DEFAULT_IMAGE forState:UIControlStateNormal];
    }

    [cell layoutSubviews];
    return cell;
}

-(IBAction)btnTapped:(UIButton *)sender {
    NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
    [arrIndexPaths addObject:selectedIndexPath];
    [self.tblVW reloadData];  // Reload Whole TableView

    //OR
    NSArray* indexArray = [NSArray arrayWithObjects:selectedIndexPath, nil];
    [self.tblVW reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationNone]; // Reload Single Row
}

【讨论】:

  • 重新加载整个 tableview 是个坏主意,但只能重新加载一个单元格
  • @AkshayDegada:用于单个单元格重新加载:self.tblVW.reloadRows(at: [selectedIndexPath as IndexPath], with: UITableViewRowAnimation.none) // SINGLE SELECTED BUTTON CELL RELOAD
  • 这是 Swift,问题标记为 objective-c
  • @Koen,现在好吗?
  • @Kuldeep 感谢关注您的 Swift 回答感谢您也发布了 Objective-c 版本,它现在可以使用了。
猜你喜欢
  • 1970-01-01
  • 2013-11-05
  • 1970-01-01
  • 1970-01-01
  • 2018-04-14
  • 1970-01-01
  • 2017-01-21
  • 2012-01-21
相关资源
最近更新 更多