Embed an UICollectionView in a UIViewController

I have been struggling the last 2 days to embed a UICollectionView in the view of UIViewVontrollers. There are indeed various objective-C examples on the Internet. With Oxygene I don’t get that up and running.

I have declared the protocols for IUICollectionViewDelegateFlowLayout and IUICollectionViewDatasource in the UIViewController class. I have not specified the interface IUICollectionViewDelegate, because it is already declared by the IUICollectionViewDelegateFlowLayout according to Apple documentation.
The required methods of the interfaces are implemented (the code I have acquired almost 1:1 from the Oxygene UICollectionViewController template and leaned on the ObjC sample codes.

Does not work however. May be, this is still a bit heavy for my iOS development entry…

If I create the IBOutlet “collectionView” and connect it in the InterfaceBuilder, then compiles and launches the project, but no CollectionView is to see in the iPhone Simulator.
There are 3 warnings, that the outlet hides the class methods (of IUICollectionViewDataSource). A class for the cell and the DataSource are present and connected in IB of cause.
I leave off the IBOutlet, then fails the build.
If I give a different name to the IBOutlet, then will my project compiled and deployed. After the SplashScreen I comes a NullPointerException.

Help. I don’t know further…
See spontaneously what I am doing wrong?

If not, then that would be a theme for Alexander’s great HowTo blog but may once again… =)

Did you have an Qxydizer support for Objective-C on your roadmap?

Thank you
Regards,
Jens

Jens,

have you looked at the CollectionView sample we ship?

Some concepts: first of all, you don’t embed a View in a View Controller. You implement a custom (Collection) View Controller, and that controller will drive the collection view itself. I’d recommend descending form UICollectionViewController to make things easier, but merely implementing the Delegate and DataSource interfaces will work too.

The core part to understand is that the collection view contains a collection view layout. Once again, you dont probably want to implement the layout yourself, but use an existing layout class, such as the UICollectionViewFlowLayout (unless you’re really know your way around collection views and really want to implement a custom layout, of course).

If you do this via Interface Builder,. what you will want to do is set your File’s Owner’s class to whatever your view controller class is called. then connect the Delegate and Data Source outlets of the collection view to your class, and in the init method of your view controller, call initWithNib…

If you wanna do this w/o IB (like the sample does, i really see no need to use a XIB for a simple collection view), just call initWithCollectionViewLayout, instead.

Does this help?

Oxidizer support for Objective-C is planned, yes, probably/hopefully for August.

Hi Marc,

yes, this helps.
Thank You.

I did a test with the PageBasedApp Template.

I added the protocols IUICollectionViewDataSource and IUICollectionViewDelegate to DataViewController’s class definition and implemented the 4 mandatory methods for the datasource and the delegate.
Also I created a custom class for the UICollectionViewCell, which holds the Outlets for my Cell Template (predefined in InterfaceBuilder).
Then I had connected the delegate and the datasource with the DataViewController (in InterfaceBuilder), selected my custom cell class and connected the cell template outlets.
At last I defined a reusable Identifier for the template cell and changed the implementation for “cellForItemAtIndexPath” as follows:

method DataViewController.collectionView(collectionView: UICollectionView) cellForItemAtIndexPath(indexPath: NSIndexPath): UICollectionViewCell;
begin
var C:MonthsInYearCollectionCellView := collectionView.dequeueReusableCellWithReuseIdentifier(‘Cell’) forIndexPath(indexPath);
C.setCellLabelText(’ I’m not an Exception :slight_smile: '); // ToDo …
result := C;
end;

That was all …
F5 >>> and everything looks good.
I have an page based Application which holds a collection view on each page.

Again…
Thank You.
It was much easyer with the right tips …