ios - UILabel connected to outlet, yet is nil -
i'm trying create screen has uiscrollview
containing uiimageviewer
, uiview
, , uitableview
. uitableview
, have custom uitableviewcell
has 2 uilabels
in it. problem is, in uitableview
's data source method, tableview(tableview, cellforrowatindexpath: indexpath)-> uitableviewcell
, after dequeue itemtablecell
, can't set text of uilabels
of cell because they're nil, reason.
here's code viewdidload()
, it's quite lengthy:
@iboutlet weak var descriptionlabel: uilabel! @iboutlet weak var scrollview: uiscrollview! var itemselected: item! var tableview = uitableview() var imageviewer: uiimageview! var firstdetailview: uiview! let detailcell = "itemdetailcell" override func viewdidload() { super.viewdidload() descriptionlabel.text = itemselected.description.capitalizedstring //setting delegates , data sources scrollview.delegate = self tableview.datasource = self tableview.delegate = self //registering cell class tableview.registerclass(itemdetailcell.self, forcellreuseidentifier: detailcell) //setting imageviewer imageviewer = uiimageview(frame: cgrectmake(0, 0, view.frame.width, view.frame.height / 2.0)) imageviewer.image = itemselected.images[1] imageviewer.contentmode = .scaleaspectfill imageviewer.clipstobounds = true //setting firstdetailview firstdetailview = uiview(frame: cgrectmake(0.0, imageviewer.frame.height, view.frame.width, 50)) firstdetailview.layer.bordercolor = uicolor.graycolor().cgcolor firstdetailview.layer.borderwidth = 0.5 //disabling autoresizing tableview, firstdetailview, , imageviewer tableview.settranslatesautoresizingmaskintoconstraints(false) firstdetailview.settranslatesautoresizingmaskintoconstraints(false) imageviewer.settranslatesautoresizingmaskintoconstraints(false) // setting itemlabel var itemlabel = uilabel() itemlabel.settranslatesautoresizingmaskintoconstraints(false) itemlabel.font = uifont(name: "helveticaneue-light", size: 14.0) ?? uifont() itemlabel.text = itemselected.title firstdetailview.addsubview(itemlabel) //setting pricelabel var pricelabel = uilabel() pricelabel.settranslatesautoresizingmaskintoconstraints(false) pricelabel.font = uifont(name: "helvetica-bold", size: 14.0) ?? uifont() pricelabel.text = "$" + string(format:"%.2f", itemselected.price) pricelabel.textalignment = .right firstdetailview.addsubview(pricelabel) //adding horizontal constraints itemlabel , pricelabel let views = dictionary(dictionaryliteral: ("itemlabel",itemlabel),("pricelabel",pricelabel)) let hconstraint = nslayoutconstraint.constraintswithvisualformat("h:|-8-[itemlabel]-[pricelabel]-8-|", options: nil, metrics: nil, views: views) firstdetailview.addconstraints(hconstraint) //adding vertical constrains itemlabel , pricelabel let itemvcontraints = nslayoutconstraint.constraintswithvisualformat("v:|[itemlabel]|", options: nil, metrics: nil, views: views) firstdetailview.addconstraints(itemvcontraints) let pricevconstraints = nslayoutconstraint.constraintswithvisualformat("v:|[pricelabel]|", options: nil, metrics: nil, views: views) firstdetailview.addconstraints(pricevconstraints) //adding subviews scrollview scrollview.addsubview(imageviewer) scrollview.addsubview(firstdetailview) scrollview.addsubview(tableview) //master views dictionary let masterviews = dictionary(dictionaryliteral: ("tableview",tableview),("firstdetailview",firstdetailview),("imageviewer",imageviewer),("scrollview",scrollview)) //setting horizontal , vertical constrains imageviewer, firstdetailview, , tableview let htblconstraints = nslayoutconstraint.constraintswithvisualformat("h:|[tableview(==scrollview)]|", options: nil, metrics: nil, views: masterviews) let himgconstrains = nslayoutconstraint.constraintswithvisualformat("h:|[imageviewer(==scrollview)]|", options: nil, metrics: nil, views: masterviews) let hfrstdtlconstraints = nslayoutconstraint.constraintswithvisualformat("h:|[firstdetailview(==scrollview)]|", options: nil, metrics: nil, views: masterviews) let vconstraints = nslayoutconstraint.constraintswithvisualformat("v:|[imageviewer(==300)]-0-[firstdetailview(==50)]-0-[tableview(==400)]|", options: nil, metrics: nil, views: masterviews) //adding constraints scrollview scrollview.addconstraints(htblconstraints) scrollview.addconstraints(himgconstrains) scrollview.addconstraints(hfrstdtlconstraints) scrollview.addconstraints(vconstraints) }
here's code uitableview
data source , delegate methods:
// mark: - table view delegate methods func tableview(tableview: uitableview, heightforrowatindexpath indexpath: nsindexpath) -> cgfloat { return cgfloat(50) } // mark: - table view data source methods func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return 4 } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier(detailcell) as! itemdetailcell cell.detailtitle = "detail:" cell.detail = "some detail." return cell }
now, problem is, code itemdetailcell
:
class itemdetailcell: uitableviewcell { @iboutlet weak var detailtitlelabel: uilabel! @iboutlet weak var detaillabel: uilabel! var detailtitle: string { { return detailtitlelabel.text! } set(newtitle){ detailtitlelabel.text = newtitle } } var detail: string { { return detaillabel.text! } set(newdetail) { detaillabel.text = newdetail } } override func awakefromnib() { super.awakefromnib() // initialization code } override func setselected(selected: bool, animated: bool) { super.setselected(selected, animated: animated) // configure view selected state } }
when run code in simulator, fatal error: unexpectedly found nil while unwrapping optional value
@ set(newtitle)
detailtitle
, stack trace shows both detailtitlelabel
, detaillabel
nil
.
what problem?
i know did wrong. sorry such long question!
i need add along these lines:
let nib = uinib(nibname: "nameofnib", bundle: nil) tableview.registernib(nib, forcellreuseidentifier: celltableidentifier)
Comments
Post a Comment