【发布时间】: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 连接了我的 delegate 和 dataSource,如下图所示:
【问题讨论】:
-
delegate连接了吗? -
添加
ccVC.delegate = self应该可以在您的initCategoriesCollection方法中解决问题 -
@holex 是的,我已经通过情节提要连接了它。我已经编辑了这篇文章,并对其进行了捕捉。 @EBDOKUM 当我尝试下一个问题时:`CategoriesCollectionViewController' 类型的值没有成员'delegate''
标签: ios swift uicollectionview