Pergunta

I am using XCode5 and iOS7. Is it possible to embed a UICollectionView into a normal UIViewController class and have the UIViewController implement the methods?

Or do I need the UICollectionViewController?

Which methods are required at minimum?

Foi útil?

Solução

Yes it is possible to implement UICollectionView without UICollectionViewController. CollectionViews are just like tableView.

As you probably already know, when you use a UITableView you have to set a data source and a delegate in order to provide the data to display and handle events (like row selection). Similarly, when you use a UICollectionView you have to set a data source and a delegate as well.
Their roles are the following:
1. The data source (UICollectionViewDataSource) returns information about the number of items in the collection view and their views.
2. The delegate (UICollectionViewDelegate) is notified when events happen such as cells being selected, highlighted, or removed.

And new to UICollectionView, you have a third protocol you must implement – a protocol specific to the layout manager you are using for the collection view.

Outras dicas

You can use a UICollectionView, you'll need the UICollectionView to conform to the UICollectionViewDelegate and UICollectionViewDataSource protocols.

so you will need at a minimum;

numberOfSectionsInCollectionView, numberOfItemsInSection and you'll need to implement cellForItemAtIndexPath to create the cells. Obviously you'll also need to define the Cell

Answer is NO. UICollectionView is subclass of UIVIew and can be added to any view you wish. Since UIVIewController has view property you can add UICollectionView to it using:

[self.view addSubview:self.collectionView];

Here is a great tutorial with sample project where you can see how to implement UICollectionView in your custom UIViewController subclass: http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12

You don't need an UICollectionViewController. Just make sure, that your ViewController implements the UICollectionViewDelegate and UICollectionViewDataSource.

This example should help you with the topic UICollectionView: http://adoptioncurve.net/archives/2012/09/a-simple-uicollectionview-tutorial/

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top