Swift 3 个选定的按钮在滚动时在 collection 视图中改变位置

Swift 3 selected button changing postition in collection view when scrolling

我正在尝试制作一个 collection 视图,其中每个单元格都包含一个图像和一个可以选择或不选择的按钮。最初一切似乎都运行良好,但是在我按下一些按钮并在视图中移动后,一些 'selected' 按钮变为 'not selected' ,反之亦然。

我发现人们在图像切换位置时遇到问题,所以我尝试缓存我的图像。我也试过完全删除图像,但问题仍然存在。

这是我的 collection 视图设置:

override func collectionView(_: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return imageArray.count

}


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell

    //cell.photo.image = imageArray[indexPath.row]

    cell.selectButton.tag = indexPath.row
    cell.selectButton.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside)

    return cell

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let length = collectionView.frame.width / 3 - 1


    return CGSize(width: length, height: length)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    //sets spacing between images
    return 1.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    //sets spacing between images
    return 1.0
}

这是我的 buttonPressed 函数:

func buttonPressed(sender: UIButton) {

    if sender.isSelected == true{
        sender.setTitleColor(UIColor.blue, for:UIControlState.normal)
        sender.isSelected = false
    }else{
        sender.setTitleColor(UIColor.red, for:UIControlState.normal)
        sender.isSelected = true
    }
}

如何解决?

您只需在 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell

中创建单元格时每次检查按钮状态

获取一个数组并在其中保留所有选定的索引

 var mArray = [Int]()


    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell

        //cell.photo.image = imageArray[indexPath.row]

        cell.selectButton.tag = indexPath.row
        cell.selectButton.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside)

//check if indexpath.row is selected
    if mArray.contains(indexPath.row){
                cell.selectButton.setTitleColor(UIColor.red, for:UIControlState.normal)
                cell.selectButton.isSelected = true
    }else{
                cell.selectButton.setTitleColor(UIColor.blue, for:UIControlState.normal)
                cell.selectButton.isSelected = false
    }


        return cell

    }


func buttonPressed(sender: UIButton) {

    if sender.isSelected == true{
        mArray.removeAtIndex(sender.tag)
        sender.setTitleColor(UIColor.blue, for:UIControlState.normal)
        sender.isSelected = false
    }else{
        mArray.append = sender.tag
        sender.setTitleColor(UIColor.red, for:UIControlState.normal)
        sender.isSelected = true
    }
}