简体   繁体   中英

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;

卡图

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 collection cell XIB won't let me add another UICollectionView to it
  • If I make a standalone UICollectionView inside the same storyboard, it won't let me add a UICollectionCell to it

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.

  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) } 

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")

}

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