ios - UICollectionView inside Static TableViewCell -
i've seen many tutorials online pertaining embedding uicollectionviews inside dynamic table view cells , subclassing collection view set delegate wondering if process different static table view cells.
i tried following example here couldn't quite follow through because seems overly complicated little no explanation. mind outlining basic steps need go through in order collection view work?
here's code far:
class featuredcontroller: uitableviewcontroller, uiscrollviewdelegate, uicollectionviewdatasource, uicollectionviewdelegate { override func viewdidload() { super.viewdidload() popularcollectionview.datasource = self popularcollectionview.delegate = self } //mark: popular events collection view @iboutlet weak var popularcollectionview: uicollectionview! func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int { return 4 } func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell { let cell = popularcollectionview.dequeuereusablecellwithreuseidentifier("popular", forindexpath: indexpath) as! uicollectionviewcell return cell } many thanks!
if know how add collection view dynamic table view cell, adding static 1 far easier. don't need sub class @ (but doing nice idea). under hood, static table view nothing more normal table view having support hosting uitableviewcontroller set have layout in interface builder automatically. so, here's how it:
- dragging collection view object library in interface builder , place in cell want.
- make sure table view hosted table view controller.
- set constraints or layout collection view in cell.
- set collection view's datasource hosted table view controller.
- adding
uicollectionviewdatasourceconformance table view controller. - implement
uicollectionviewdatasource's methods, namelycollectionview:numberofitemsinsection:,collectionview:cellforitematindexpath:inuitableviewcontroller.
if know how work uitableview or uicollectionview in general, should not hard follow.
update code looks should have worked. so, should check whether:
- you've set class of table view controller
featuredcontrollerclass. - you have wired collection view in interface builder
popularcollectionview. - you have prototype collection view cell identifier
popular. although, should crash if haven't done so. - in ib, have set table view static.
i have small example did here

the orange view collection view greenish view prototype collection view cell identifier mycell.

and set view controller collection view's data source. set in code did too.
then implement data source methods below.
@interface viewcontroller () <uicollectionviewdatasource> @end @implementation viewcontroller - (nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section { return 20; } - (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath { uicollectionviewcell *cell = [collectionview dequeuereusablecellwithreuseidentifier:@"mycell" forindexpath:indexpath]; return cell; } @end and result:

Comments
Post a Comment