【问题标题】:Clicking an image in collection view selects other images单击集合视图中的图像会选择其他图像
【发布时间】:2020-08-31 15:16:22
【问题描述】:

我有以下代码:

import UIKit
import Photos
import PhotosUI

private let reuseIdentifier = "Cell"

class CollectionVC: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    var imageArray = [UIImage]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

     grapPhotos()
       

        
    }


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

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath)
        let imageView = cell.viewWithTag(1) as! UIImageView
        
        collectionView.allowsMultipleSelection = true
        cell.layer.cornerRadius = 4
        imageView.image = imageArray[indexPath.row]
    
        return cell
    }
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if let cell = collectionView.cellForItem(at: indexPath) {
          cell.layer.borderColor = #colorLiteral(red: 0.1411764771, green: 0.3960784376, blue: 0.5647059083, alpha: 1)
          cell.layer.borderWidth = 5
        
        }
        
        
    }
    
    override func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        if let cell = collectionView.cellForItem(at: indexPath) {
          cell.layer.borderColor = #colorLiteral(red: 0.1411764771, green: 0.3960784376, blue: 0.5647059083, alpha: 1)
          cell.layer.borderWidth = 0
        }
    }
    
    func grapPhotos() {
        
        let imgManager = PHImageManager.default()
        let requestOptions = PHImageRequestOptions()
        requestOptions.isSynchronous = true
        requestOptions.deliveryMode = .highQualityFormat
        
        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        fetchOptions.predicate = NSPredicate(format: "mediaType = %d || mediaType = %d", PHAssetMediaType.image.rawValue, PHAssetMediaType.video.rawValue)
        
        if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: fetchOptions) {
            
            if fetchResult.count > 0 {
                
                for i in 0..<fetchResult.count {
                    imgManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {
                        
                        image, error in
                        
                        self.imageArray.append(image!)
                        
                        
                    })
        
                    
                }
            }
            else {
                self.collectionView?.reloadData()
                print("No Photos")
            }
        }
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let width = collectionView.frame.width / 3 - 6
        
        
        
        return CGSize(width: width, height: width)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        
        return 6.0
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        
        return 6.0
    }

}

我很抱歉,但我真的不知道我哪里出错了。这是我的 CollectionViewController 的所有代码,然后在情节提要中,我有一个集合 VC,其中一个单元格中有一个图像,标签为 1,然后一个单元格的标识符为 "Cell"。我遇到的问题是当我在手机上运行代码时,选择一个图像最终会选择其他图像。例如:我选择了第一张图片,然后我向下滚动并且已经选择了另一张图片。我做了一点测试,前 21 张图像几乎是独一无二的。但是,就好像图像几乎具有相同的ID。就像选择第一个图像一样,也选择了第 22 个图像。顺便说一句,我正在构建一个自定义图像选择器。

【问题讨论】:

  • 细胞被重复使用。调用 cellForRow 时,如果需要,您应该删除边框。
  • 我很困惑你说什么是 cellForRow?
  • 我的意思是unc collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath),“cellForRow”是UITableView的版本,但是复用的概念是一样的。
  • 如果你看我的代码,我就有这个功能,这就是我拥有关于单元格的所有信息的地方
  • @Larme 我仍然对你说的话感到困惑

标签: swift uicollectionview phasset phimagemanager


【解决方案1】:

这里的问题不止一个,但您抱怨的主要问题是因为您忘记了单元格被重复使用。您需要在cellForItem 中配置单元格,而不是在didSelectdidDeselect 中。

当您的didSelect 转身直接与物理单元对话时,它完全是在做错事。相反,didSelectdidDeselect 应该与您的 数据模型 对话,在模型的对象中设置一些属性,即应该考虑选择 这些 索引路径中的对象,而 这些其他 索引路径中的对象不应该。然后重新加载数据。

然后,当cellForItem 出现时,它必须完全配置其单元格,根据数据模型和索引路径显示其选定的外观或未选择的外观。这样,如果一个单元格位于先前选定的行中,但现在在未选定的行中重复使用,则它具有所需的未选定外观。

换句话说,只有cellForItem 应该考虑单元格。其他人应该只考虑索引路径(或者更好的是,考虑与数据相关的一些唯一标识符,但这是另一回事)。

【讨论】:

  • 我理解你所说的关于细胞被重复使用的说法是有道理的,尽管我不知道该怎么做。
猜你喜欢
  • 1970-01-01
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
  • 1970-01-01
相关资源
最近更新 更多