Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionViewCell在选择后未更新视图_Ios_Uicollectionview - Fatal编程技术网

Ios UICollectionViewCell在选择后未更新视图

Ios UICollectionViewCell在选择后未更新视图,ios,uicollectionview,Ios,Uicollectionview,出现以下问题: override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 在选择项时调用,但我无法从此方法更改任何单元格属性 我使用精简的UICollectionViewController创建了一个新项目,以在选中时更改单元格的背景色。它也不起作用。这是: import UIKit private let reuseIdentifier = "

出现以下问题:

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
在选择项时调用,但我无法从此方法更改任何单元格属性

我使用精简的UICollectionViewController创建了一个新项目,以在选中时更改单元格的背景色。它也不起作用。这是:

import UIKit

private let reuseIdentifier = "Cell"

class CollectionViewController: UICollectionViewController {

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 5
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)

    cell.backgroundColor = UIColor.blue

    return cell
}

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = self.collectionView(self.collectionView, cellForItemAt: indexPath)
    cell.backgroundColor = UIColor.green
}
}

我在Storyboard中所做的唯一一件事就是删除标准视图控制器并将其替换为UICollectionViewController,创建UICollectionViewController的子类,并将Storyboard中的控制器设置为该类

此外,当从
didSelectItemAt
方法内部调用时,我可以确认此方法返回了单元格的索引路径:

self.collectionView.indexPathsForSelectedItems

您使用了错误的API从不调用委托方法
collectionView(\uCellForItemat:
),使用
cellForItem(at:

if let cell = collectionView.cellForItem(at: indexPath) {
   cell.backgroundColor = UIColor.green
}

但请注意,此更改不是持久性的。当用户滚动时,颜色将变回蓝色。

您可以通过以下方式实现此更改:

视图控制器

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ColoursViewCell", for: indexPath) as! ColoursViewCell
    return cell
}
UICollectionViewCell

class ColoursViewCell: UICollectionViewCell {
    @IBOutlet var photoImageView: UIImageView?

    override var bounds: CGRect {
        didSet {
            self.layoutIfNeeded()
        }
    }

    override var isSelected: Bool{
        didSet{
            if self.isSelected{
                self.photoImageView?.backgroundColor = UIColor.random
            }else{
                self.photoImageView?.backgroundColor = UIColor.lightGray
            }
        }
    }
}
您可以从我在GitHub中的链接中获得示例项目