手势识别器不能在其他UICollectionViewCell

时间:2016-03-16 16:06:12

标签: ios swift uicollectionview

我创建了一个UICollectionView作为主要的UICollectionView,每个collectionViewCell都有另一个UICollectionView“B”作为它的子视图 然后我在“B”中添加了tapGesture。但“B”无法触发此手势。 以下是我的主要代码:

主要UICollectionView

import Foundation
import UIKit

class mainViewController :UIViewController,UICollectionViewDataSource,UICollectionViewDelegate {

    @IBOutlet weak var mainCollectionView: UICollectionView!

    override func viewDidLoad() {
        self.mainCollectionView.registerNib(UINib(nibName: "OuterCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "OuterCollectionViewCell")
        self.mainCollectionView.delegate = self
        self.mainCollectionView.dataSource = self
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return 1
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("OuterCollectionViewCell", forIndexPath: indexPath) as! OuterCollectionViewCell
        return cell
    }

}

UICollectionView B:

class OuterCollectionViewCell: UICollectionViewCell,UICollectionViewDataSource, UICollectionViewDelegate {

    @IBOutlet weak var InnerCollectionView: UICollectionView!

    override func awakeFromNib() {
        super.awakeFromNib()
        InnerCollectionView.registerNib(UINib(nibName: "InnerCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CellInner")
    }

    func handleTapGesture(sender: UITapGestureRecognizer) {
        print("tapguesture")
    }

    override func didMoveToWindow(){
        let tapGesture = UITapGestureRecognizer(target: self, action: "handleTapGesture:")
        InnerCollectionView.backgroundColor = UIColor.redColor()
        InnerCollectionView.addGestureRecognizer(tapGesture)
        InnerCollectionView.delegate = self
        InnerCollectionView.dataSource = self

    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CellInner", forIndexPath: indexPath) as! InnerCollectionViewCell
        return cell
    }
}

这个问题困扰了我一整天。有没有人对如何解决这个问题有任何想法?

我已将项目上传到github:https://github.com/dengchicheng/Test2

更新------------------------------

今天我通过代码可编程地初始化innerCollectionView而不是由NIB初始化。它的工作原理

let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
let InnerCollectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 800, height: 600), collectionViewLayout: flowLayout)

我不知道NIB对UIcollectionview做了什么 我确实在界面构建器

中将userInteractive设置为true

0 个答案:

没有答案