Swift 3 选择按钮在滚动时更改集合视图中的位置



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

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

这是我的集合视图设置:

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
}

这是我的按钮按下功能:

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

最新更新