zardon
zardon

Reputation: 1651

How do I add a UICollectionView inside a UICollectionViewCell?

I have a UICollectionViewCell which I created using a visual XIB file.

The collection view is horizontally displayed.

I'm wanting to add another UICollectionView to this cell in the footer of the cell.

The cell itself is a "card" representing some information;

This diagram helps illustrate what I'm after;

Card diagram

The idea is that I can show/hide the dice imagery as many as needed, including zero; in a horizontal fashion as a child partial inside the current UICollectionCell.

Attempts using Storyboards;

The only solution I've got so far is to manually create 5 imageviews, then attach them to a collection outlet; and then I would need to do a for-loop to show/hide each die imagery.

Upvotes: 0

Views: 1878

Answers (2)

Optimus
Optimus

Reputation: 810

You don't need tableview for that just in your collectionview cell xib drag another collection and set its scrolling property to vertical,then create xib from inner collectionview.

In outer collectionview cell use that method to set delegate and datasource

// code
override func awakeFromNib() {
   super.awakeFromNib()
   serInnerCollectionView()
}

 func serInnerCollectionView() {

 collectionViewInner.delegate = self
   collectionViewInner.dataSource = self
   collectionViewInner.register(UINib(nibName: "  FooterCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "FooterCollectionViewCellIdentifier")

}

Upvotes: 1

Michał Kwiecień
Michał Kwiecień

Reputation: 2874

  1. Create normal View Controller with Collection View in the interface builder
  2. Create CollectionViewCell in separate XIB. Inside this cell insert another CollectionView
  3. In first VC register that collectionViewCell as sectionHeader:

    tableView.register(UINib(nibName: DashboardPagerHeader.cellIdentifier, bundle: nil), forHeaderFooterViewReuseIdentifier: DashboardPagerHeader.cellIdentifier)
    
  4. Create another CollectionViewCell in separate xib like before

  5. Inside class of first collection view cell in awakeFromNib register cell created in step 4 like that:

    override func awakeFromNib() {
       super.awakeFromNib()
       collectionView.delegate = self
       collectionView.dataSource = self
       collectionView.register(UINib(nibName: "  DashboardHeaderCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: DashboardHeaderCollectionViewCell.cellIdentifier)
    }
    

Upvotes: 2

Related Questions