繁体   English   中英

UICollectionViewCell在collectionview的多个单元格中可见一个变化

[英]UICollectionViewCell change in one seen in multiple cells in collectionview

我有一个带有collectionviewcells的uicollectionview,每个单元格都有一个与“收藏夹”按钮相关联的布尔值。 垂直放置的单元格超过50个(一次可以看到四个单元格)。 如果单击“收藏夹”按钮,它将在突出显示的图像和未突出显示的图像之间切换。

该功能有效,但是由于某些原因,当我单击一个然后向下滚动时,我看到其他单元格突出显示了其收藏夹按钮。 当我向上滚动时,“收藏夹”按钮不再突出显示。

该代码中缺少什么吗?

注意:默认情况下,我将每个单元格的布尔值设置为false。 仅当我单击单元格的“收藏夹”按钮时,它才会更改。

我的代码如下:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! SimpleDispensarySubCell
        cell.backgroundColor = UIColor.init(white: 0.10, alpha: 0.25)
        cell.infoLine2TextVw.text = ""
        cell.infoLine3TextVw.text = ""
        if let heading_name = self.dict_dict_holder[indexPath.item]["Name"]{
            cell.headerTextVw.text = heading_name
            cell.infoLine1TextVw.text = self.dict_dict_holder[indexPath.item]["Phone"]
        }

        if cell.isFavorite{
            cell.isFavorite = true
            cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_fill_icon"), for: .normal)
        }
        else{
            cell.isFavorite = false
            cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_nofill_icon"), for: .normal)
        }
        cell.bringSubview(toFront: cell.headerTextVw)
        //cell.favorite_button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(AddFavorite(withSender:))))
        cell.favorite_button.addTarget(self, action:#selector(AddFavorite), for: .touchUpInside)
        return cell
    }

    @objc func AddFavorite(withSender sender:UIButton){
        let cell = sender.superview as! SimpleDispensarySubCell

        if cell.isFavorite{
            cell.isFavorite = false
            cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_nofill_icon"), for: .normal)
        }
        else{
            cell.isFavorite = true
            cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_fill_icon"), for: .normal)
        }

    }

这是因为您使用的是collectionView.dequeueReusableCell您应该定义一个数组以保存每个单元格的收藏状态。 它可以解决您的问题。

let favoriteStateArray = countOfRows;

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! SimpleDispensarySubCell
    cell.backgroundColor = UIColor.init(white: 0.10, alpha: 0.25)
    cell.infoLine2TextVw.text = ""
    cell.infoLine3TextVw.text = ""
    if let heading_name = self.dict_dict_holder[indexPath.item]["Name"]{
        cell.headerTextVw.text = heading_name
        cell.infoLine1TextVw.text = self.dict_dict_holder[indexPath.item]["Phone"]
    }

    if favoriteStateArray[indexPath.row]{
        cell.isFavorite = true
        cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_fill_icon"), for: .normal)
    }
    else{
        cell.isFavorite = false
        cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_nofill_icon"), for: .normal)
    }
    cell.bringSubview(toFront: cell.headerTextVw)
    //cell.favorite_button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(AddFavorite(withSender:))))
    cell.favorite_button.addTarget(self, action:#selector(AddFavorite), for: .touchUpInside)
    return cell
}

@objc func AddFavorite(withSender sender:UIButton){
    let cell = sender.superview as! SimpleDispensarySubCell
    let index = collectionView.indexPath(for: cell)
    if favoriteStateArray[indexPath.row]{
        favoriteStateArray[indexPath.row] = false
        cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_nofill_icon"), for: .normal)
    }
    else{
        favoriteStateArray[indexPath.row] = false
        cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_fill_icon"), for: .normal)
    }

}

由于单元正在重用,因此更改会影响多个单元。 您可以使用“准备重用”方法来清除仅影响特定单元格的更改,然后清除该单元格并为重用做准备。

override func prepareForReuse() {

     cell.isFavorite = false

    super.prepareForReuse()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM