didSelectItem 未被调用

didSelectItem not being called

我的 didSelectItemAt 方法没有被调用,控制台也没有打印任何内容。我打开了用户交互,但仍然无法打印出任何内容。我不确定是我的自定义 PinterestStyle 布局导致了这个问题,还是我遗漏了什么。最终目标是进入显示所选单元格配置文件页面的详细视图控制器。我将使用 prepareForSegue 来做到这一点,但是我什至无法让它在点击时打印出单元格的名称。

class PagesCollectionViewController: UICollectionViewController, firebaseHelperDelegate {

var storageRef: StorageReference!{
    return Storage.storage().reference()
}
var usersList = [String]()
var authService : FirebaseHelper!
var userArray : [Users] = []
var images: [UIImage] = []

var names: [String] = []

override func viewWillAppear(_ animated: Bool) {

    if Global.Location != "" && Global.Location != nil
    {
        usersList = Global.usersListSent
        print(usersList)
        self.authService.ListOfUserByLocation(locationName: Global.Location, type: .ListByLocation)
    }
}

override func viewDidLoad() {
    self.collectionView?.allowsSelection = true
    self.collectionView?.isUserInteractionEnabled = true
    super.viewDidLoad()
    self.authService = FirebaseHelper(viewController: self)
    self.authService.delegate = self
    setupCollectionViewInsets()
    setupLayout()
}

private func setupCollectionViewInsets() {
    collectionView!.backgroundColor = .white
    collectionView!.contentInset = UIEdgeInsets(
        top: 20,
        left: 5,
        bottom: 49,
        right: 5
    )
}

private func setupLayout() {
    let layout: PinterestLayout = {
        if let layout = collectionViewLayout as? PinterestLayout {
            return layout
        }
        let layout = PinterestLayout()

        collectionView?.collectionViewLayout = layout

        return layout
    }()
    layout.delegate = self
    layout.cellPadding = 5
    layout.numberOfColumns = 2
}

func firebaseCallCompleted(data: AnyObject?, isSuccess: Bool, error: Error?, type: FirebaseCallType) {
    if(type == .ListByLocation) {

        if(isSuccess) {
            self.userArray.removeAll()
            self.images.removeAll()
            self.images.removeAll()

            if(data != nil) {

                let dataDict = data as! NSDictionary
                let keyArray = dataDict.allKeys

                for i in 0 ..< keyArray.count {

                    var dict = NSDictionary()
                    dict = dataDict.object(forKey: keyArray[i]) as! NSDictionary
                    self.userArray.append(Users.init(data: dict))
                }
            }

            self.collectionView?.reloadData()

        }
        else {
            print(error?.localizedDescription)
            SVProgressHUD.dismiss()
        }
    }

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

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print(userArray[indexPath.row].name)
}



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

    cell.nameLabel.text = userArray[indexPath.row].name

    if let imageOld = URL(string: userArray[indexPath.row].photoURL){

        cell.photo.sd_setImage(
            with: imageOld,
            placeholderImage: nil,
            options: [.continueInBackground, .progressiveDownload]
        )

    }

    return cell
   }

  }


   extension PagesCollectionViewController : PinterestLayoutDelegate {

func collectionView(collectionView: UICollectionView,
                    heightForImageAtIndexPath indexPath: IndexPath,
                    withWidth: CGFloat) -> CGFloat {
    var image: UIImage?

    let url = URL(string: userArray[indexPath.row].photoURL)
    let data = try? Data(contentsOf: url!)
    image = UIImage(data: data!)

    return (image?.height(forWidth: withWidth))!
}
func collectionView(collectionView: UICollectionView,
                    heightForAnnotationAtIndexPath indexPath: IndexPath,
                    withWidth: CGFloat) -> CGFloat {
    return 30
}
}

检查2个条件:-

  • 确保您已将委托设置为 UICollectionView
  • 确保 PageCollectionCell 中的内容类似于未启用用户交互的图像。如果启用图像用户交互,则不会调用 didSelectItemAt。

正如 Manish Mahajan 所说,快速解决方法是:

在 cellForItemAt 函数中将 contentView 设置为不可点击

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

    cell.contentView.isUserInteractionEnabled = false

    return cell
}