【问题标题】:didSelectItemAtIndexPath of UICollectionView is not called未调用 UICollectionView 的 didSelectItemAtIndexPath
【发布时间】:2016-09-13 15:33:07
【问题描述】:

经过几天的疯狂,我无法找到解决问题的方法。问题是这样的:在UICollectionView 中选择一个单元格后,不会调用didSelectItemAtIndexPath 方法。

我的视图结构是:

此视图由具有以下方法的控制器管理:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
}

override func viewDidLoad() {
    super.viewDidLoad()

    initCategoriesCollection()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func initCategoriesCollection(){
    let ccVC : CategoriesCollectionViewController  =  UIStoryboard(name:"CandidateProfile",bundle:nil).instantiateViewControllerWithIdentifier("categoriesController") as! CategoriesCollectionViewController;
    addChildViewController(ccVC)
    ccVC.view.frame = CGRectMake(0, 0, self.containerView.frame.size.width, self.containerView.frame.size.height);
    containerView.addSubview(ccVC.view)

    ccVC.didMoveToParentViewController(self)

}    

上面的容器视图结构如下:

这个容器视图由一个 ViewController 管理,它实现了这些方法:

// tell the collection view how many cells to make
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.e1Categories.count
}

// make a cell for each cell index path
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    // get a reference to our storyboard cell
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CategoryCollectionViewCell

    // Use the outlet in our custom class to get a reference to the UILabel in the cell
    cell.categoryName.text = self.e1Categories[indexPath.item].string
    cell.categoryName.numberOfLines = 0
    cell.categoryName.sizeToFit()
    cell.categoryName.textAlignment = .Center
    cell.categoryCircle.makeCircle()

    return cell
}

// MARK: - UICollectionViewDelegate protocol

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    // handle tap events
    print("You selected cell #\(indexPath.item)!")
}

有人知道发生了什么吗?我试图删除 ScrollView 因为它可能正在拦截触摸事件,但它仍然无法正常工作。 在阅读了其他一些问题后,在 Stackoverflow 中,没有一个问题适合我。

提前致谢。

编辑:

我已经通过 Storyboard 连接了我的 delegatedataSource,如下图所示:

【问题讨论】:

  • delegate 连接了吗?
  • 添加 ccVC.delegate = self 应该可以在您的 initCategoriesCollection 方法中解决问题
  • @holex 是的,我已经通过情节提要连接了它。我已经编辑了这篇文章,并对其进行了捕捉。 @EBDOKUM 当我尝试下一个问题时:`CategoriesCollectionViewController' 类型的值没有成员'delegate''

标签: ios swift uicollectionview


【解决方案1】:

在您的initCategoriesCollection() 中,您正在创建一个新的CategoriesCollectionViewController

let ccVC : CategoriesCollectionViewController  =  UIStoryboard(name:"CandidateProfile",bundle:nil).instantiateViewControllerWithIdentifier("categoriesController") as! CategoriesCollectionViewController;

根据你的说法:

这个容器视图由一个 ViewController 管理,它实现了这些方法:

然后您将ccVC 添加到另一个实现delegate func didSelectItemAtIndexPathcontroller。因此,故事板上设置的delegate 与管理它的正确controller 不一致。

您应该更新delegate,因为您要将它添加到另一个正在实施它的controller。尝试更新到这个

let ccVC : CategoriesCollectionViewController  =  UIStoryboard(name:"CandidateProfile",bundle:nil).instantiateViewControllerWithIdentifier("categoriesController") as! CategoriesCollectionViewController;
addChildViewController(ccVC)
ccvc.collectionView.delegate = self

【讨论】:

    【解决方案2】:
    collectionView.delegate = self
    collectionView.dataSource = self
    

    设置委托和数据源 UIViewController 类,而不是在情节提要中

    【讨论】:

      【解决方案3】:

      首先感谢您给我提示以解决我的问题。跟随他们并尝试将父控制器分配为ccvc.collectionViewdelegate,它表示子ViewController 中没有任何delegate。 我的问题是,我认为如果通过Storyboard 同时分配delegatedataSource 会成功,但不会。

      所以我决定在父控制器中同时实现 Delegate 和 Datasource 协议,删除子控制器,它现在就像一个魅力。 也许我在使用Storyboard 时误解了概念。感谢您的帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多