Can´t add items to UICollectionView inside UIView xib

You can't have UICollectionViewCell when the UICollectionView is on a Nib. What you need to do is to create the UICollectionViewCell as another nib and get it registered in the class that you are using for your CollectionView.

Create a new nib, drag a UICollectionViewCell inside it, and do something like this in the class that works with your UICollectionView.

override func awakeFromNib() {
    let nibName = UINib(nibName: "ClassCollectionCell", bundle:nil)
    collectionView.registerNib(nibName, forCellWithReuseIdentifier: "collectionCell")
}

Remember you can add a custom class to the UICollectionViewCell so you can pass dynamic data to it.


Adding cells in a xib is not supported. If you must use a xib file, then you will need a separate xib which contains the UICollectionView cell. Storyboards may be a better solution.

It is not clear what you are trying to achieve. UICollectionView has specific means for creating headers which uses the datasource and delegate. Collection views are good for displaying items in a grid layout or other complex arrangements.

If all you need is to display a list of rows, then a UITableViewController might be an easier alternative.

Whatever the case, it is probably better to use a storyboard instead of a xib, and to subclass the UICollectionViewController or UITableViewController, rather than a subview.

Your custom class name can be entered in the identity inspector for the UIViewController or UIView:

UIViewController identity inspector

UIView identity inspector