UICollectionView didSelectItemAt 方法不起作用

UICollectionView didSelectItemAt method doesn't work

当我点击 UICollectionView 中的项目时,我需要给出一些逻辑,但我的 didSelectItemAt 方法不起作用

这是我的ViewController

代码
class NewGalleryViewController: UIViewController {
var presenter: ViewToPresenterPhotoProtocol?
var builder: GalleryRequestBuilder?

@IBOutlet var collectionView: UICollectionView!
let refreshControl = UIRefreshControl()

let reuseIdentifier = "customCVCell"


@objc func refresh() {
    presenter?.refresh()
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.collectionView.refreshControl = refreshControl
    refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
    self.setupPresenterIfNeed()
    presenter?.viewDidLoad()
    // Do any additional setup after loading the view.
}

func setupPresenterIfNeed() {
    self.collectionView.backgroundColor = UIColor.white
    if self.presenter == nil {
        let presenter = GalleryPresenter()
        presenter.view = self
        self.presenter = presenter
        self.builder = GalleryRequestBuilder()
    }
}
}

extension NewGalleryViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.presenter?.photos.count ?? 0
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PhotoCollectionViewCell
    
    cell.cellSetup(photo: (self.presenter?.photos[indexPath.item])!)
    
    if indexPath.row == (self.presenter?.photos.count)! - 1 {
        self.presenter?.fetchNewPhotos()
    }
    
    return cell
}

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 180, height: 128)
}

private func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    // handle tap events
    print("test Tap")
}

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

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

extension NewGalleryViewController: PresenterToViewPhotoProtocol{
func onFetchPhotoSuccess() {
    self.collectionView.reloadData()
    self.collectionView!.collectionViewLayout.invalidateLayout()
    self.collectionView!.layoutSubviews()
    self.refreshControl.endRefreshing()
}

func onFetchPhotoFailure(error: String) {
    print("View receives the response from Presenter with error: \(error)")
    self.collectionView.refreshControl?.endRefreshing()
}


}

这是我的 CollectioViewCell 代码

class PhotoCollectionViewCell: UICollectionViewCell {
var builder = GalleryRequestBuilder()

@IBOutlet weak var imageView: UIImageView!

override func awakeFromNib() {
    super.awakeFromNib()
    self.imageView.isUserInteractionEnabled = false
    // Initialization code
}

@objc func navigateToDetailView() {
    
}

func cellSetup(photo: Photo) {
    self.imageView.kf.setImage(with: builder.createImageUrl(name: photo.image.name))
    self.imageView.layer.cornerRadius = 8
    
    self.contentView.layer.cornerRadius = 8
    
    self.layer.borderColor = UIColor.opaqueSeparator.cgColor
    self.layer.borderWidth = 1

    
    self.layer.shadowColor = UIColor.black.cgColor
    self.layer.shadowOffset = CGSize(width: 0.0, height: 3.0)
    self.layer.shadowOpacity = 0.3
    self.layer.shadowRadius = 0.0
    self.layer.cornerRadius = 8.0
    self.layer.masksToBounds = false
}

}

我认为问题可能出在 ImageView 中,但删除它后 didSelectItemAt 方法仍然无法正常工作。

如果您需要更多信息,这里是 GitHub 存储库 https://github.com/Feetelcore/SwiftWebantGallery

我发现你不见了delegate。在 viewDidload 中添加此行,然后重试:

collectionView.delegate = self

问题是您将 didSelectItemAtIndexPath 函数定义为私有。当您这样做时,它不代表 UICollectionView 的委托函数。您应该从函数中删除 private 并像下面这样定义。

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("siski")    }