Ios UICollectionView上的奇怪细胞反应

Ios UICollectionView上的奇怪细胞反应,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我对一个包含UICollectionView的VC有一个非常奇怪的问题。我正在尝试让用户先前选择的单元格,1)减少其alpha值,2)取消隐藏单元格左上角的图标。我的收藏装载了两次。一次使用之前存储在设备中的数据源,然后从API加载新数据 我发现,起初视图加载正确,但奇怪的是,每个单元格中的alpha颜色都正确,但有些单元格在第二次加载时取消隐藏图标 我不知道是什么原因造成的。有什么帮助吗 func collectionView(collectionView: UICollectionView,

我对一个包含UICollectionView的VC有一个非常奇怪的问题。我正在尝试让用户先前选择的单元格,1)减少其alpha值,2)取消隐藏单元格左上角的图标。我的收藏装载了两次。一次使用之前存储在设备中的数据源,然后从API加载新数据

我发现,起初视图加载正确,但奇怪的是,每个单元格中的alpha颜色都正确,但有些单元格在第二次加载时取消隐藏图标

我不知道是什么原因造成的。有什么帮助吗

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)

    if defaults.boolForKey("gotPL") {

        let catNumber = cell.viewWithTag(1) as! UILabel
        let catTitle = cell.viewWithTag(2) as! UILabel

        catNumber.text = String(indexPath.item + 1)
        catTitle.text = defaults.arrayForKey("playlistTitles")![indexPath.item] as? String

        cell.layer.cornerRadius = 30

//CUSTOM LOAD FUNCTIONALITY STARTS HERE
        if defaults.arrayForKey("selectedArray") != nil {
            let selectedArray: [String] = defaults.arrayForKey("selectedArray") as! [String]
            if selectedArray.contains(defaults.arrayForKey("playlistTitles")![indexPath.item] as! String) {
                print(indexPath.item)
                cell.alpha = 0.5 //WORKS CORRECTLY
                cell.viewWithTag(3)?.hidden = false //IS REPLICATED INCORRECTLY ON SECOND LOAD
            }
        }

        return cell
    }

    return cell
}

您需要刷新每个单元格的
单元格.alpha
单元格.alpha
值,而不是
条件单元格条件。原因是细胞重用

    cell.alpha = 1 
    cell.viewWithTag(3)?.hidden = true
    if defaults.arrayForKey("selectedArray") != nil {
                let selectedArray: [String] = defaults.arrayForKey("selectedArray") as! [String]
                if selectedArray.contains(defaults.arrayForKey("playlistTitles")![indexPath.item] as! String) {
                    print(indexPath.item)
                    cell.alpha = 0.5 //WORKS CORRECTLY
                    cell.viewWithTag(3)?.hidden = false //IS REPLICATED INCORRECTLY ON SECOND LOAD
                }
            }

祝你好运

您需要刷新每个单元格的
cell.alpha
cell.alpha
值,而不是
if
条件单元格条件。原因是细胞重用

    cell.alpha = 1 
    cell.viewWithTag(3)?.hidden = true
    if defaults.arrayForKey("selectedArray") != nil {
                let selectedArray: [String] = defaults.arrayForKey("selectedArray") as! [String]
                if selectedArray.contains(defaults.arrayForKey("playlistTitles")![indexPath.item] as! String) {
                    print(indexPath.item)
                    cell.alpha = 0.5 //WORKS CORRECTLY
                    cell.viewWithTag(3)?.hidden = false //IS REPLICATED INCORRECTLY ON SECOND LOAD
                }
            }

祝你好运

工作得很有魅力!非常感谢你的帮助!工作得很有魅力!非常感谢你的帮助!