UICollectionView didSelectItemAtIndexPath影响多个单元格-swift Swift,无情节提要,来自xib文件的UICollectionView

UICollectionView didSelectItemAtIndexPath影响多个单元格-swift Swift,无情节提要,来自xib文件的UICollectionView,swift,uicollectionview,Swift,Uicollectionview,我正在运行非常简单的代码来更新单元格的背景色,但是,不止一个单元格正在更新 override func viewDidLoad(){ super.viewDidLoad() // getting images from library images = PHAsset.fetchAssetsWithMediaType(.Image, options: nil) collectionView.allowsMultipleSelection = fa

我正在运行非常简单的代码来更新单元格的背景色,但是,不止一个单元格正在更新

        override func viewDidLoad(){
    super.viewDidLoad()
    // getting images from library
    images = PHAsset.fetchAssetsWithMediaType(.Image, options: nil)

    collectionView.allowsMultipleSelection = false
    var nipName2 = UINib(nibName: "CollectionViewCell", bundle:nil)
    collectionView.registerNib(nipName2, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderCell")
    var nipName = UINib(nibName: "MyViewCell", bundle:nil)
    collectionView.registerNib(nipName, forCellWithReuseIdentifier: "Cell")
 }

 func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
 }

 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return images.count
 }

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! MyViewCell
    cell.frame.size.width = 60
    return cell
 }

 func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView{
    switch kind
    {
    case UICollectionElementKindSectionHeader:
        let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "HeaderCell", forIndexPath: indexPath) as! CollectionViewCell
        return headerView
    default:
        assert(false, "Unexpected element kind")
    }
 }

 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
 {
    var cell = collectionView.cellForItemAtIndexPath(indexPath)
    cell?.backgroundColor = UIColor.redColor()
 }

我有大约300个不同的细胞。我只想更新第三个单元格,但其他单元格的背景会随机变化。

因此集合视图和表视图具有很强的视图重用概念。这使得性能大大提高,因为它不必同时在内存中保留[在您的情况下]300个单元

当您在某些单元格上设置背景色时,滚动时这些单元格可能会被重复使用。由于您没有在显示的视图上显式设置背景,因此它只使用当前的背景

要解决此问题,请在请求视图时设置颜色:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
   // This does not guarantee to be a fresh, new cell, can be reused
   var cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! MyViewCell
   cell.frame.size.width = 60
   // Explicitly set the background color:
   cell.backgroundColor = .whiteColor() // or whatever your default color is
   return cell
}

现在很明显,这将导致额外的副作用。假设您将单元格的背景色更改为红色,然后滚动到别处。当你回来时,你现在正在将它设置回白色。也就是说,您需要跟踪选择了哪些单元格(可能是通过存储它们的索引),并适当地设置它们的颜色。

我建议覆盖
UICollectionViewCell的
setSelected:animated:
。是否允许在
UICollectionView
中进行多个选择如果这是您的意思,“collectionView.MultipleToTouchEnabled=false”不起作用。-“collectionView.allowsMultipleSelection=false“也不起作用。我不是说
allowsMultipleSelection
问题在于重复使用单元格,请确保为所有其他不希望被选中的单元格重置背景。”。使用此方法为选定的一个启用后台,并为所有其他func cellForItemAtIndexPath(indexPath:NSIndexPath)->UICollectionViewCell禁用后台?