如何处理 UICollectionView 的多项选择,它会在滑动时更改数据?

How to handle multiple selection for a UICollectionView, which changes data on swipe?

我在 UIViewController 上有一个 UICollectionView,它可以在滑动时更改数据,它也有搜索功能。我更改数组值并在每次滑动时重新加载 UICollectionView。现在,当我尝试实现多选时,滚动时选择会发生变化。

我使用的代码是,

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

        if searchIsOn {
            return filteredTeamNames.count
        }
        else{
            return teamNames.count
        }        
}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierTwo, for: indexPath as IndexPath) as! HouseCells

        cell.layer.masksToBounds = true
        cell.layer.cornerRadius = 6            

        var singleComponent = [String : String]()
        if searchIsOn {
             singleComponent = self.filteredTeamNames[indexPath.item]
        } else {
             singleComponent = self.teamNames[indexPath.item]
        }
        let stringURL: String = singleComponent["icon_profile"]!

        cell.teamImage.sd_setImage(with: URL(string: stringURL), placeholderImage: UIImage(named: "demo"))
        cell.teamNameLabel.text = singleComponent["username"]

        //to check for bg color
        let idValue: Int = Int(singleComponent["id"]!)!
        print(idValue)

        if selectedTeamIDs.contains(idValue) {
            cell.backgroundColor = UIColor(red:0/255, green:0/255, blue:0/255, alpha:1.0)
            cell.layer.borderWidth = 2.0
            cell.layer.borderColor = UIColor(red:56/255, green:192/255, blue:201/255, alpha:1.0).cgColor

        } else {
            cell.backgroundColor = UIColor(red:26/255, green:26/255, blue:26/255, alpha:0.4)

        }

        return cell

}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        var singleComponent = [String : String]()
        if searchIsOn {
            singleComponent = self.filteredTeamNames[indexPath.item]

        } else {
            singleComponent = self.teamNames[indexPath.item]

        }
        let idTempValue: Int = Int(singleComponent["id"]!)!
        print(idTempValue)

        if !selectedTeamIDs.contains(idTempValue) {
            selectedTeamIDs.append(idTempValue)

        }
        print(selectedTeamIDs)

        middleSectionCollView.reloadData()

    }

数组 selectedTeamIDs 包含唯一编号,因此如果用户单击特定索引,我会将相应的 ID 添加到 selectedTeamIDs 数组并在 cellForItemAt 中,我正在检查是否当前索引值的 id 存在于 selectedTeamIDs 数组中,如果是,我正在更改单元格的背景颜色。但是我的代码不起作用,滚动时选择发生变化。我在哪里缺少逻辑?

首先不要reloadDatadidSelectItemAt方法里面,移除reload行

自定义单元格中的第二个 class HouseCells(顺便说一下 class 的名称应该是单数)

添加此代码

  override var isSelected: Bool {
        didSet {

            if isSelected {
                backgroundColor = UIColor(red:0/255, green:0/255, blue:0/255, alpha:1.0)
                layer.borderWidth = 2.0
                layer.borderColor = UIColor(red:56/255, green:192/255, blue:201/255, alpha:1.0).cgColor

            } else {
                backgroundColor = UIColor(red:26/255, green:26/255, blue:26/255, alpha:0.4)

            }


        }
    }

同时从 cellForItemAt 方法中删除此代码

if selectedTeamIDs.contains(idValue) {
            cell.backgroundColor = UIColor(red:0/255, green:0/255, blue:0/255, alpha:1.0)
            cell.layer.borderWidth = 2.0
            cell.layer.borderColor = UIColor(red:56/255, green:192/255, blue:201/255, alpha:1.0).cgColor

        } else {
            cell.backgroundColor = UIColor(red:26/255, green:26/255, blue:26/255, alpha:0.4)

        }

并且在您的 didSelectItemAt 方法中您现在不需要此代码:

if !selectedTeamIDs.contains(idTempValue) {
            selectedTeamIDs.append(idTempValue)

        }
        print(selectedTeamIDs)

        middleSectionCollView.reloadData()

给定的代码是正确的,除了 else 部分中两行代码的问题,如

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

        if searchIsOn {
            return filteredTeamNames.count
        }
        else{
            return teamNames.count
        }        
}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierTwo, for: indexPath as IndexPath) as! HouseCells

        cell.layer.masksToBounds = true
        cell.layer.cornerRadius = 6            

        var singleComponent = [String : String]()
        if searchIsOn {
             singleComponent = self.filteredTeamNames[indexPath.item]
        } else {
             singleComponent = self.teamNames[indexPath.item]
        }
        let stringURL: String = singleComponent["icon_profile"]!

        cell.teamImage.sd_setImage(with: URL(string: stringURL), placeholderImage: UIImage(named: "demo"))
        cell.teamNameLabel.text = singleComponent["username"]

        //to check for bg color
        let idValue: Int = Int(singleComponent["id"]!)!
        print(idValue)

        if selectedTeamIDs.contains(idValue) {
            cell.backgroundColor = UIColor(red:0/255, green:0/255, blue:0/255, alpha:1.0)
            cell.layer.borderWidth = 2.0
            cell.layer.borderColor = UIColor(red:56/255, green:192/255, blue:201/255, alpha:1.0).cgColor

        } else { cell.backgroundColor = UIColor(red:26/255, green:26/255, blue:26/255, alpha:0.4) 
                 cell.layer.borderWidth = 2.0
                 cell.layer.borderColor = UIColor.clear.cgColor
} 

        return cell

}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        var singleComponent = [String : String]()
        if searchIsOn {
            singleComponent = self.filteredTeamNames[indexPath.item]

        } else {
            singleComponent = self.teamNames[indexPath.item]

        }
        let idTempValue: Int = Int(singleComponent["id"]!)!
        print(idTempValue)

        if !selectedTeamIDs.contains(idTempValue) {
            selectedTeamIDs.append(idTempValue)

        }
        print(selectedTeamIDs)

        middleSectionCollView.reloadData()

    }

希望这个回答对某人有用!