当滚动时,Swift 3选择按钮在集合视图中更改postition

时间:2017-03-25 17:00:13

标签: ios swift uicollectionview uicollectionviewcell

我正在尝试创建一个集合视图,其中每个单元格包含一个图像和一个可以选择或不选择的按钮。最初一切似乎都运行良好,但是在我按下一些按钮并在视图中移动之后,一些选择了'按钮更改为“未选中”'反之亦然。

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

这是我的集合视图设置:

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

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您需要的是在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
    }
}