简体   繁体   English

这是为什么? 按钮切换自定义单元格的状态,该单元格的背景图像被其他单元格重用

[英]Why is that? Button toggles the state on a custom cell whose background image is reused by other cells

Button on a custom cell to toggle the background image state, which is reused by other cells, should be reuse problem, but do not know how to solve? 自定义单元格上的按钮切换背景图像状态,其他单元格已重用该按钮,应该是重用问题,但不知道该如何解决? Can you give me some advice? 你能给我一些建议吗?

    @objc func LickCheck(_ sender:UIButton){
        //Toggle button background image
        if !sender.isSelected {
          //code
        }else{
         //code
        }
        sender.isSelected = !sender.isSelected

    }

       func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell : FoneCell  = tableView.dequeueReusableCell(withIdentifier: "Focid", for: indexPath) as! FoneCell;

        cell.xxBtn.tag = indexPath.row;

        cell.xxBtn.addTarget(self, action: #selector(LickCheck), for: UIControl.Event.touchUpInside);

        return cell;
        }

FoneCell.swift: FoneCell.swift:

lazy var xxxBtn : UIButton = {
        let btn = UIButton();
        btn.setImage(UIImage.init(named: "love_18x18_"), for: UIControl.State.normal);
        btn.setImage(UIImage.init(named: "love_on_20x20_"), for: UIControl.State.selected)


        return btn;
    }();

This should be something like, 这应该是这样的,

FoneCell.swift : FoneCell.swift

protocol FoneCellDelegate: class {
   func didSelectButton(atIndex: Int)
}

@IBOutlet var loveBtn: UIButton!

public weak var delegate: FoneCellDelegate?

func awakeFromNib() {
   super.awakeFromNib()
   loveBtn.addTarget(self, action: #selector(LickCheck), for: UIControl.Event.touchUpInside);
}

 @objc func LickCheck(_ sender:UIButton){
        //Toggle button background image
        if !sender.isSelected {
            addDate(sender.tag)
        }else{
            deleteDate(sender.tag)
        }
        sender.isSelected = !sender.isSelected
        delegate?.didSelectButton(atIndex: sender.tag)
    }

ViewController.swift ViewController.swift

var buttonStates =  Array(repeating: false, count: 10)//if you need 10 rows

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return buttonStates.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell : FoneCell  = tableView.dequeueReusableCell(withIdentifier: "Focid", for: indexPath) as! FoneCell;

    cell.loveBtn.tag = indexPath.row;
    cell.loveBtn.isSelected = buttonStates[indexPath.row]
    cell.delegate = self // Implement protocol on cell's class. And update the value in buttonStates when state toggled
    return cell
}

func didSelectButton(atIndex: Int) {
      buttonStates[atIndex] = !buttonStates[atIndex]
      tableView.reloadData()
}

in override method of UITableViewCell 在UITableViewCell的重写方法中

override func prepareForReuse() {
          super.prepareForReuse()
      //set default state here 
     self.imageView.image = nil
     self.toggleButton.isOn = false 
//.....
    }

or you can do the same in tableViewcellForRowAtIndexPath 或者您可以在tableViewcellForRowAtIndexPath中执行相同操作

 let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell

    //Set default behaviour here
    cell.imageView.image = nil 
    cell.toggleButton.isOn = false 
  ....
return cell 

PS you can make array in viewController for save the state of button selected. PS,您可以在viewController中创建数组以保存所选按钮的状态。 eg 例如

let isSelectedArray = Array(repeating: false, count: 100)
@objc func LickCheck(_ sender:UIButton){
        //Toggle button background image
        let tag = sender.tag
        if !isSelectedArray[tag]{

          //code
        }else{
         //code
        }
        isSelectedArray[tag] = !isSelectedArray[tag]
        sender.isSelected = !sender.isSelected
  }

I've solved the problem, First, define an array of global records that clicked over the button 我已经解决了这个问题,首先,定义一个单击按钮的全局记录数组

    lazy var numbercells:[Int] = []

Secondly, in the 其次,在

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell : FoneCell  = tableView.dequeueReusableCell(withIdentifier: "Focid", for: indexPath) as! FoneCell;

        if self.numbercells.contains(indexPath.row){
            cell.loveBtn.isSelected = true;
        }else{
            cell.loveBtn.isSelected = false;

        }
        cell.loveBtn.addTarget(self, action: #selector(LickCheck), for: UIControl.Event.touchUpInside);
        cell.loveBtn.tag = indexPath.row;

        return cell;

    }

The last 最后

    @objc func LickCheck(_ sender:UIButton){

        if !sender.isSelected {
            self.numbercells.append(sender.tag);
            addDate(sender.tag)
        }else{
            self.numbercells = self.numbercells.filter(){$0 != sender.tag}
            deleteDate(sender.tag)
        }
        let posinton = IndexPath(row: sender.tag, section: 0);
        self.tableView.reloadRows(at: [posinton], with: UITableView.RowAnimation.none)

    }

This solves the button select state reuse problem on the cell 这解决了单元格上的按钮选择状态重用问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM