簡體   English   中英

未調用 UICollectionView 的 didSelectItemAtIndexPath

[英]didSelectItemAtIndexPath of UICollectionView is not called

經過幾天的瘋狂,我無法找到解決問題的方法。 問題是這樣的:在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 ,如下圖所示:

在此處輸入圖片說明

在您的initCategoriesCollection() ,您正在創建一個新的CategoriesCollectionViewController

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

照你說的:

這個容器視圖由實現這些方法的 ViewController 管理:

然后,您將ccVC添加到另一個實現delegate func didSelectItemAtIndexPath controller 因此,故事板上設置的delegate不是管理它的正確controller

您應該更新delegate因為您將它添加到另一個正在實現它的controller 嘗試更新到這個

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

設置委托和數據源 UIViewController 類而不是故事板

首先感謝您給我解決問題的提示。 跟隨他們並嘗試將父控制器分配為ccvc.collectionView delegate ,它說在子 ViewController 中沒有任何delegate 我的問題是,我認為如果通過Storyboard同時分配delegatedataSource ,它會起作用,但不會。

所以我決定在父控制器中實現 Delegate 和 Datasource 協議,刪除子控制器,它現在就像一個魅力。 也許我在使用Storyboard時誤解了概念。 謝謝你的幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM