ios - Can't access labels in a custom cell -
ios - Can't access labels in a custom cell -
i have viewcontroller , tableviewcontroller on storyboard. tableviewcontroller has custom cell label on it. custom cell has own class companytableviewcell.swift:
import uikit class companytableviewcell: uitableviewcell { @iboutlet var companyname : uilabel init(style: uitableviewcellstyle, reuseidentifier: string) { super.init(style: style, reuseidentifier: reuseidentifier) println("cell's initialised") } ... // other default cell methods }
in tableviewcontroller have methods:
override func viewdidload() { super.viewdidload() self.tableview.registerclass(companytableviewcell.self, forcellreuseidentifier: "companycell") println("cell's registered") } override func tableview(tableview: uitableview!, cellforrowatindexpath indexpath: nsindexpath!) -> uitableviewcell? { allow cell = tableview.dequeuereusablecellwithidentifier("companycell") companytableviewcell println(cell.companyname) cell.companyname.text = "company name" homecoming cell }
when run code, if tableviewcontroller marked initial view controller runs ok , text of label on custom cell beingness changed successfully. can see bunch of cells company name
.
but when assign initial view controller property viewcontroller, , trying access tableviewcontroller (the viewcontroller has button runs searchbyname action):
// viewcontroller.swift @ibaction func searchbyname(sender : uibutton) { var vc = searchtableviewcontroller(style: uitableviewstyle.plain) vc.companies = self.companies self.showviewcontroller(vc, sender: self) }
i've got app crunch on cell.companyname.text = "company name"
string error: fatal error: can't disclose optional.none. on previous string println(cell.companyname)
prints nil console, although in console see cell class registered , cell object initialised.
the question how assign text label, i've forgotten? , why works in first case not in second. thanks.
@iboutlet var companyname : uilabel
outlet , if phone call var vc = searchtableviewcontroller(style: uitableviewstyle.plain)
outlet companyname label not initialised , hence nil , hence accessing companyname label causes crash.
but in first case initialising viewcontroller storyboard having searchtableviewcontroller scene contains companytableviewcell outlet companyname , connected cell ,hence initialised , there fore works expected.
solution :
if using storyboard utilize instantiateviewcontrollerwithidentifier:
method initialise viewcontroller(assuming searchtableviewcontroller has companytableviewcell in storyboard).
if doing programatically override initialiser method of companytableviewcell create label , add together cell.
ios swift
Comments
Post a Comment