gpt4 book ai didi

ios - 未调用 UICollectionView 的 didSelectItemAtIndexPath

转载 作者:行者123 更新时间:2023-11-28 16:11:42 25 4
gpt4 key购买 nike

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

我的观点结构是:

Views structure

此 View 由具有以下方法的 Controller 管理:

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)

}

上面的容器 View 具有以下结构:

enter image description here

这个容器 View 由实现这些方法的 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,如下图所示:

enter image description here

最佳答案

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

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

据你说:

And this container view is managed by a ViewController which implements these methods:

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

您应该更新 delegate,因为您要将它添加到另一个实现它的 controller。尝试对此进行更新

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

关于ios - 未调用 UICollectionView 的 didSelectItemAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39473886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com