ios - Pass parameter when initialising UICollectionViewCell -
i have uicollectionviewcell subclass , trying pass value cell when initialise it.
class additemcell: uicollectionviewcell { var additembutton = uibutton() var collectionelement : pfobject? override init(frame: cgrect) { super.init(frame: frame) additembutton = uibutton.buttonwithtype(uibuttontype.custom) as! uibutton additembutton.frame = cgrectmake(0, 0, 170, 300) additembutton.backgroundcolor = uicolor.redcolor() additembutton.addtarget(self, action: "additemtocollectionelement:", forcontrolevents: uicontrolevents.touchupinside) additembutton.settitle("new item", forstate: uicontrolstate.normal) additembutton.titlelabel!.font = uifont(name: "cochin-bolditalic", size: 20) self.addsubview(additembutton) } init (frame: cgrect, passedcollectionelement : pfobject){ self.collectionelement = passedcollectionelement super.init(frame: frame) } func additemtocollectionelement (sender : uibutton!) { } required init(coder adecoder: nscoder) { super.init(coder: adecoder) } }
clearly method being called overridden one. there way call second / other way pass variable value?
if want initialize object parameters , make overridden init called you'll need use convenience initializer follow:
convenience init (frame: cgrect, passedcollectionelement : pfobject){ self.init(frame: frame) self.collectionelement = passedcollectionelement }
this way calling let cell = additemcell(frame:someframe, passedcollectionelement : someelement)
initialize object calling both convenience , designated initializers. can learn more initialization here
Comments
Post a Comment