Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom data source class using storyboard

I can't find the way to add my custom data source class of UICollectionView in the storyboard. The data source class has an interface like this:

@interface CalendarDataSource : NSObject<UICollectionViewDataSource>

I try to find the calendar data source in the scene sidebar but I can't. So I can't seem to link the class to my UICollectionView. Is there something I should do but I didn't?


If you want to reproduce the similar problem, just create a new project in XCode5, set up a UICollectionViewController, and create a new class just like above.

I'm currently rewriting the project described in http://www.objc.io/issue-3/ The Github repo is https://github.com/objcio/issue-3-collection-view-layouts This project managed to do this, however. My code is almost the same to the project but can't run because viewcontroller.collectionView.dataSource is uninitialized.

Doing the initialisation programmatically is another issue, but please tell me in the comment because all I got from the internet is setting dataSource to self. But setting it to self doesn't require introducing a new retained data source object, which I got stuck on.

like image 726
jchnxu Avatar asked Jul 17 '14 20:07

jchnxu


1 Answers

Go to the Interface Builder.

  • Right-click on your Collection View in the storyboard, delete dataSource outlet (if any).
  • Type "Object" in the search text field of the Objects Library and drag an 'Object' to your view controller containing UICollectionView (i.e. do the same as you add any view or view controller to storyboard scene, with the exception you add not a view or view controller but an abstract object).
  • In the left-side 'Scenes' panel of your storyboard, highlight just added Object; in right-side panel go to the 'Identity Inspector' and type CalendarDataSource instead of pre-defined NSObject. In left side panel ('Scenes'), 'Object' will be renamed to 'Calendar Data Source' automatically.
  • In the left-side 'Scenes' panel of your storyboard, control-drag from your UICollectionView to the 'Calendar Data Source'; in pop-up appeared, select dataSource outlet.

You've done!

enter image description here

like image 132
Nikolay Mamaev Avatar answered Sep 22 '22 10:09

Nikolay Mamaev