didSelectItem未被调用

时间:2018-04-03 02:50:36

标签: ios swift uicollectionview didselectrowatindexpath

我的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 个答案:

答案 0 :(得分:4)

检查2个条件: -

  • 确保已将委托设置为集合视图
  • 确保PageCollectionCell中的内容与图像没有启用用户交互。如果启用了图像用户交互,则didSelectAtItem将不会调用。

答案 1 :(得分:2)

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

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

var basketLine = db.BasketLines
        .Include(b => b.Product)
        .FirstOrDefault(b => b.BasketId == BasketId && b.ProductId == productId);