ios - Subclassing UICollectionViewCell and initialising from xib -
i registering cell this:
collectionview!.registernib(uinib(nibname: "itemcell", bundle: nil), forcellwithreuseidentifier: "itemcellidentifier")
and accessing this:
let cell = collectionview.dequeuereusablecellwithreuseidentifier("itemcellidentifier", forindexpath: indexpath) as! itemcell
itemcell subclass of uicollectionviewcell:
class itemcell: uicollectionviewcell { @iboutlet weak var itemimage: uiimageview! @iboutlet weak var itemimagedescription: uilabel! //creation nib requires method overridden override func awakefromnib() { super.awakefromnib() // initialization code } required init(coder adecoder: nscoder) { super.init(coder: adecoder) } }
finally have xib uicollectionviewcell class itemcell.
i keep getting error:
assertion failure in -[uicollectionview _dequeuereusableviewofkind:withidentifier:forindexpath:viewcategory:], /sourcecache/uikit/uikit-3347.44.2/uicollectionview.m:3443.
the crash on line:
let cell = collectionview.dequeuereusablecellwithreuseidentifier("itemcellidentifier", forindexpath: indexpath) as! itemcell
edit
collectionview!.registernib(uinib(nibname: "itemcell", bundle: nil), forcellwithreuseidentifier: "itemcellidentifier")
is called from
override init(style: uitableviewcellstyle, reuseidentifier: string?) { super.init(style: style, reuseidentifier: reuseidentifier) }
contained in:
class collectioncell : uitableviewcell, uicollectionviewdatasource, uicollectionviewdelegate {
where calling registernib
?
i reproduced setup match can tell yours.
this works me, no errors :
import uikit class viewcontroller: uiviewcontroller, uicollectionviewdatasource, uicollectionviewdelegate { var model:[string] = [] @iboutlet weak var collectionview: uicollectionview! override func viewdidload() { super.viewdidload() // simple test model ... index in 1...100 { model.append("\(index)") } // register nib collectionview.registernib(uinib(nibname: "itemcell", bundle: nil), forcellwithreuseidentifier: "itemcell") // test layout var layout = uicollectionviewflowlayout() layout.itemsize = cgsize(width:200.0,height:200.0) layout.scrolldirection = uicollectionviewscrolldirection.vertical self.collectionview.collectionviewlayout = layout self.collectionview.delegate = self; self.collectionview.datasource = self; self.collectionview.reloaddata() } // mark: - uicollectionviewdatasource func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int{ return model.count } func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell{ var cell:uicollectionviewcell = collectionview.dequeuereusablecellwithreuseidentifier("itemcell", forindexpath: indexpath) as! uicollectionviewcell return cell } }
Comments
Post a Comment