繁体   English   中英

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

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

自定义单元格上的按钮切换背景图像状态,其他单元格已重用该按钮,应该是重用问题,但不知道该如何解决? 你能给我一些建议吗?

    @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:

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;
    }();

这应该是这样的,

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

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()
}

在UITableViewCell的重写方法中

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

或者您可以在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,您可以在viewController中创建数组以保存所选按钮的状态。 例如

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
  }

我已经解决了这个问题,首先,定义一个单击按钮的全局记录数组

    lazy var numbercells:[Int] = []

其次,在

    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;

    }

最后

    @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)

    }

这解决了单元格上的按钮选择状态重用问题

暂无
暂无

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

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