如何阻止我的 tableView 单元重用其中的按钮? [英] How can i stop my tableView cell from reusing the button inside it?

查看:33
本文介绍了如何阻止我的 tableView 单元重用其中的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在协议中有一个函数,它将 TableViewcell 作为参数.

I have a function inside a protocol that takes a TableViewcell as an argument.

protocol GoingButtonDelegate {
    func goingButtonPressed(cell: TableViewCell)
}

class TableViewCell: UITableViewCell {
    // Outlets
    @IBOutlet weak var goingButton: UIButton!

    var delegate: GoingButtonDelegate?

    @IBAction func goingButtonTapped(_ sender: Any) {
        delegate?.goingButtonPressed(cell: self)  
    }

然后我转到我的 ViewController 并实现委托和它的功能,即在点击时更改按钮的图像.goingSelected"是绿色图像,goingDeselected"是红色图像.

I then go over to my ViewController and implement the delegate and it's function, which is to change the image of a button when tapped. The "goingSelected" is a green image and the "goingDeselected" is a red image.

这一切都很好,当点击单元格的按钮时,它会从红色变为绿色,反之亦然.但是,当单元格被重用时,单元格的按钮状态会持续存在并被重新用于进入视图的新行.有什么办法可以阻止这种情况发生吗?

This all works out fine, when tapped the button of a cell goes from red to green and vice versa. However, when the cell gets reused, the button state of the cell persists and gets reused for the new row that enters view. Is there any way to stop this from happening?

 extension ViewController: GoingButtonDelegate {
    func goingButtonPressed(cell: TableViewCell) {
        cell.goingButton.isSelected = !cell.goingButton.isSelected

        if cell.goingButton.isSelected == true {
            cell.goingButton.setImage(UIImage(named: "goingSelected"), for: UIControlState.selected)
        } else if cell.goingButton.isSelected == false {
            cell.goingButton.setImage(UIImage(named: "goingDeselected"), for: UIControlState.normal)
        }
    }
}

推荐答案

您需要将选定的行存储在索引路径数组中,在此之前我认为您应该进行一些改进……或者很多!

You need to store the selected rows in an array of index paths, before that I think you should make few enhancements ... or a lot!!

单元本身应该处理它的按钮,控制器应该只跟踪所有单元的状态.将这两个属性添加到您的单元格

the cell itself should handle it's button, the controller should just keep track of all cells status. Add these two properties to your cell

class TableViewCell: UITableViewCell {
    var indexPath:IndexPath?
    var isSelected : Bool = false {
        didSet{
            if isSelected {
                cell.goingButton.setImage(UIImage(named: "goingSelected"), for: UIControlState.normal)
            } else {
                cell.goingButton.setImage(UIImage(named: "goingDeselected"), for: UIControlState.normal)
            }
        }
    }
    // Outlets
    @IBOutlet weak var goingButton: UIButton!

    var delegate: GoingButtonDelegate?

    @IBAction func goingButtonTapped(_ sender: Any) {
        self.isSelected = !self.isSelected
        delegate?.goingButtonPressed(cell: self)  
    }
    ..
    ...
}

并将选定的单元格存储在您的视图控制器中以跟踪每个单元格的状态.

And store the selected cells in your view controller to keep track of each cell status.

extension ViewController: GoingButtonDelegate {
    var selectedCells = NSMutableArray()
    func goingButtonPressed(cell: TableViewCell) {
        if cell.isSelected {
            selectedCells.add(cell.indexPath)
        } else {
            selectedCells.remove(cell.indexPath)
        }
    }
}

并在您的行单元格"方法中添加一个小的更改

and in your "cell for row" method just add a small change

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier") as! TableViewCell
    cell.indexPath = indexPath
    cell.isSelected = selectedCells.contains(indexPath)
    ..
    ...
    return cell
}

这篇关于如何阻止我的 tableView 单元重用其中的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆