Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionView重用单元格背景问题_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios UICollectionView重用单元格背景问题

Ios UICollectionView重用单元格背景问题,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我使用UICollectionview显示4个按钮,用户应选择1个按钮。 我创建了uibutton的自定义子类: class CustomButton: UIButton { override var isSelected: Bool{ didSet{ backgroundColor = isSelected ? Constants.Colors.selectedMenu: Constants.Colors.menuCell }

我使用UICollectionview显示4个按钮,用户应选择1个按钮。 我创建了uibutton的自定义子类:

class CustomButton: UIButton {

    override var isSelected: Bool{
        didSet{
            backgroundColor =  isSelected ? Constants.Colors.selectedMenu: Constants.Colors.menuCell
        }
    }

}
自定义单元格类:

class SymptomCell: UICollectionViewCell {

    @IBOutlet weak var optionButton1: UIButton!
    @IBOutlet weak var optionButton2: UIButton!
    @IBOutlet weak var optionButton3: UIButton!
    @IBOutlet weak var optionButton4: UIButton!


    func configCell(options :  [String]){
        optionButton1.setTitle(options[0], for: .normal)
        optionButton2.setTitle(options[1], for: .normal)
        optionButton3.setTitle(options[2], for: .normal)
        optionButton4.setTitle(options[3], for: .normal)
    }

    @IBAction func clickAction(_ sender: UIButton) {
         sender.isSelected = sender.isSelected ? false : true
    }
}
例如,我看到,若我选择按钮并滚动几个单元格,那个么即使我并没有按下按钮,同一索引中的按钮也会得到不同的背景色

在我尝试使用的cell类中:

 override func prepareForReuse() {
        optionButton1.isSelected = false
        optionButton2.isSelected = false
       // ...
 }
但每次都会取消选择所有项目。
我能做什么?

尝试在自定义单元格中使用prepareForReuse并设置backgroundColor=nil

override func prepareForReuse() {

    backgroundColor =  nil
    optionButton1.isSelected = false
}

UICollectionViewCell
内部的任何
UIView
在单元重用之间都必须是无状态的,因此,您必须保留
prepareforeuse
函数

但是,
UICollectionViewCell
本身正在恢复其
isSelected
状态,当它在以前选择的
indepath
中被重用时

因此,方法可以是与按钮一起选择单元格。 在你的牢房里应该有这样的东西:

protocol MyCellDelegate : NSObjectProtocol {
    func optionButton1DidToggle(in cell: UICollectionViewCell?)
}

class MyCell: UICollectionViewCell {

    var delegate : MyCellDelegate?

    @IBAction func clickAction(_ sender: UIButton) {
        // you can assign delegate to your cell, the same as UICollectionViewDelegate, and forward here click callback to your delegate,
        delegate?.optionButton1DidToggle(in: self) 
        //.....
    }

    override var isSelected: Bool {
        didSet{
            optionButton1.isSelected = self.isSelected
        }
    }

    override func prepareForReuse() {
        optionButton1.isSelected = false
    }
}
MyCellDelegate
实现:

public func optionButton1DidToggle(in cell: UICollectionViewCell?) {
    if !cell.optionButton1.isSelected {
        collectionView.selectItem(at: collectionView.indexPath(for: cell) animated:false scrollPosition:.centeredHorizontally)
    } else {
        collectionView.deselectItem(at: collectionView.indexPath(for: cell) animated:false)
    }
}

你确定它会取消全部选择吗?@LeangSocheat当我滑动到下一个单元格时,它会取消所有按钮。你的gif看起来像页面视图控制器或滚动视图。lolz。你可以用这个按下你的保持代码吗?@leangSochet现在检查一下,如果我有超过1个“optionButton”怎么办?你是说一个单元格中有多个按钮?然后,您需要在
UICollectionViewDataSource
后面有一个模型,其中每个模型对象都将为每个
indexPath
存储单元格按钮的状态。您将在
UICollectionViewDataSource
collectionView(\uCellForItemat:indexPath)
函数中设置按钮的状态。