swift - UITableViewCell subclass IBOutlets returns nil -
i've set uitableviewcell subclass storyboard , connected subviews iboutlets, can check here:
public class waitingstatustableviewcell: uitableviewcell { @iboutlet weak var leftboxview: uiview! @iboutlet weak var lefttimelabel: uilabel! @iboutlet weak var lefttitlelabel: uilabel!
overrided initialisers:
override init(style: uitableviewcellstyle, reuseidentifier: string?) { super.init(style: style, reuseidentifier: reuseidentifier) self.initialconfigure() } required public init(coder adecoder: nscoder) { super.init(coder: adecoder) self.initialconfigure() }
and on initialconfigure function try configure properties of subviews
func initialconfigure() { self.leftboxview.backgroundcolor = uicolor.clearcolor() self.leftboxview.layer.shadowcolor = uicolor.darkgraycolor().cgcolor self.leftboxview.layer.shadowpath = uibezierpath(roundedrect: bounds, cornerradius: 12.0).cgpath self.leftboxview.layer.shadowoffset = cgsize(width: 2.0, height: 2.0) self.leftboxview.layer.shadowopacity = 1.0 self.leftboxview.layer.shadowradius = 2 self.leftboxview.layer.maskstobounds = true self.leftboxview.clipstobounds = false }
it doesn't work @ all, receive following error:
fatal error: unexpectedly found nil while unwrapping optional value
any ideas?
oh, way, works on drawsrect function, want understand "why"
it comes down order of execution:
here key moments occur in sequence when view loaded:
init()
called on table view subclass , instance of class created. @ time iboutletsnil
. that's why you're getting error.- other subviews of scene initialized, iboutlets linked , assigned values, , instance added view hierarchy.
awakefromnib()
called, @ point iboutlets no longer havenil
value long have been linked through storyboard.drawrect()
called last, when view ready drawn screen
so, solution move self.initialconfigure()
awakefromnib
method:
override func awakefromnib() { super.awakefromnib() self.initialconfigure() }
and trust ios work magic.
here excellent article detailing sequence of events occur when view created, if want know more on subject.
Comments
Post a Comment