swift - NSCollectionView dataSource not working properly -
i'm experimenting new nscollectionview
api, introduced in el capitan.
following wwdc video, create subclass of nscollectionviewflowlayout
determine layout of collection view.
class layout : nscollectionviewflowlayout { override func preparelayout() { super.preparelayout() self.minimuminteritemspacing = 0 self.minimumlinespacing = 0 let cheight = self.collectionview!.bounds.height let cwidth = self.collectionview!.bounds.width self.itemsize = cgsizemake(cwidth / 2.0, cheight / 6.0) } }
after that, i've created nsobject
subclass serve data source.
class datasource : nsobject, nscollectionviewdatasource { func collectionview(collectionview: nscollectionview, numberofitemsinsection section: int) -> int { return 5 } func collectionview(collectionview: nscollectionview, itemforrepresentedobjectatindexpath indexpath: nsindexpath) -> nscollectionviewitem { /* doesn't called! */ let item = collectionview.makeitemwithidentifier("cell", forindexpath: indexpath) item.view.wantslayer = true item.view.layer?.backgroundcolor = nscolor.redcolor().cgcolor return item } }
the issue collectionview:itemforrepresentedobjectatindexpath:
never gets called.
this how initialise collection view:
let collectionview = nscollectionview(frame: view.bounds) let datasource = datasource() let layout = layout() collectionview.collectionviewlayout = layout collectionview.registerclass(nscollectionviewitem.self, foritemwithidentifier: "cell") collectionview.datasource = datasource collectionview.backgroundcolors = [.blackcolor()]
i can see collection view in superview, there no cells.
also, line, if called outside delegate (but after cell class registered) makes app crash!
let item = collectionview.makeitemwithidentifier("cell", forindexpath: /*any index path*/)
am doing wrong or nscollectionview
new api broken?
thanks.
the method makeitemwithidentifier crucial , looks .xib named "cell" in bundle: may need register registernib:foritemwithidentifier: if comes bundle.
thus problem comes before that, since collectionview:itemforrepresentedobjectatindexpath never gets called , arise pattern: why create new class serve datasource , not use same nsviewcontroller nscollectionview resides?
i , new api works perfectly. hope you!
Comments
Post a Comment