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 iboutlets nil. 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 have nil 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

Popular posts from this blog

c# - Where does the .ToList() go in LINQ query result -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -