简体   繁体   中英

UICollectionView delegate not being called

I have custom UICollectionViewController , UICollectionViewFlowLayout , and UICollectionViewCell classes. In my layout I have stacked each section similar to 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. 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.

Under my UICollectionViewController class I have specified protocols <UICollectionViewDelegate, UICollectionViewDataSource> . In viewDidLoad I have also specified:

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. It's class type is set to my custom class, and I have set the ReuseIdentifier . In my view controller I load my custom cell class and use the same ReuseIdentifier . However there is a disconnect between StoryBoard and the Cell class. 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. I'm not sure why this is the case since Storyboard clearly shows my prototype cell has the correct class type.

Answered my own question - items created in storyboard call initWithCoder instead of 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM