[英]UICollectionView delegate not being called
I have custom UICollectionViewController
, UICollectionViewFlowLayout
, and UICollectionViewCell
classes. 我具有自定义
UICollectionViewController
, UICollectionViewFlowLayout
和UICollectionViewCell
类。 In my layout I have stacked each section similar to http://skeuo.com/uicollectionview-custom-layout-tutorial . 在我的布局中,我堆叠了类似于http://skeuo.com/uicollectionview-custom-layout-tutorial的每个部分。
Currently all data is displayed correctly when I run the app. 目前,当我运行该应用程序时,所有数据都可以正确显示。
I am trying to implement didSelectItemAtIndexPath
in my controller to initiate a seque based on the tapped item. 我试图在我的控制器中实现
didSelectItemAtIndexPath
,以基于被轻击的项启动序列。 However the delegate method never gets called. 但是,委托方法永远不会被调用。 Inside my custom cell class I have added a
touchesEnded
method that DOES get called, but I can not figure out why the delegate in my view controller class is not firing. 在我的自定义单元类中,我添加了一个确实被调用的
touchesEnded
方法,但是我无法弄清楚为什么视图控制器类中的委托没有触发。
Under my UICollectionViewController
class I have specified protocols <UICollectionViewDelegate, UICollectionViewDataSource>
. 在我的
UICollectionViewController
类下,我指定了协议<UICollectionViewDelegate, UICollectionViewDataSource>
。 In viewDidLoad
I have also specified: 在
viewDidLoad
我还指定了:
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
As a side note, but I think it's a related problem - in Storyboard I have my prototype cell. 作为附带说明,但我认为这是一个相关的问题-在Storyboard中,我有我的原型单元。 It's class type is set to my custom class, and I have set the
ReuseIdentifier
. 它的类类型设置为我的自定义类,并且我已经设置了
ReuseIdentifier
。 In my view controller I load my custom cell class and use the same ReuseIdentifier
. 在我的视图控制器中,加载我的自定义单元格类并使用相同的
ReuseIdentifier
。 However there is a disconnect between StoryBoard and the Cell class. 但是,StoryBoard和Cell类之间没有连接。 I can make changes to the storyboard prototype cell and they are not reflected in what happens at runtime.
我可以对情节提要板原型单元进行更改,并且这些更改不会反映在运行时发生的情况中。 Instead, I have to comment out the line:
相反,我必须注释掉这一行:
[self.collectionView registerClass:[NSideCollectionViewCell class] forCellWithReuseIdentifier:NSIDE_CELL_IDENTIFIER];
To force it to use my storyboard cell version. 强制它使用我的情节提要单元版本。 But when I do that, my
initWithFrame
custom initialization code in my cell class never gets called. 但是当我这样做时,我的单元类中的
initWithFrame
自定义初始化代码永远不会被调用。 I'm not sure why this is the case since Storyboard clearly shows my prototype cell has the correct class type. 我不确定为什么会这样,因为Storyboard清楚地显示了我的原型单元具有正确的类类型。
Answered my own question - items created in storyboard call initWithCoder instead of initWithFrame. 回答了我自己的问题-在情节提要中创建的项目调用initWithCoder而不是initWithFrame。 Commenting out the explicit custom cell class registration allows the view controller to follow what was set up in storyboard and everything works as expected.
注释掉显式的自定义单元格类注册,使视图控制器可以遵循在情节提要中设置的内容,并且一切均按预期进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.