简体   繁体   中英

How to wire up UICollectionView in a custom UIView - Swift

I'm dealing with a strange issue where I cannot wire up UICollectionView when I want to set it up in a custom UIView.

The reason for having it in UIView instead of UIViewController is that I am creating several custom Views and I'm showing only one per time.

Now, the issue I have is that I am able to drag UICollectionView inside my custom UIView nib but I cannot drop any UICollectionViewCell on it. However, when I do the same thing inside a UIView of a UIVIewController, not only that I am able to drop a cell on it - the cell appears by default.

This is what appears when i place collection view inside a view controller: 在此处输入图片说明

This is when I add it to a custom UIView 在此处输入图片说明

SO my question is, how to add a cell inside a view?

You can't do this in view's xib file. Create a separate xib file for your cell and register it in collection view:

let cellNib = UINib(nibName: "YourCellNibFileName", bundle: nil)
collectionView.registerNib(cellNib, forCellWithReuseIdentifier: "YourCellIdentifier")

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