Tuesday, 15 June 2010

ios - Access to Custom UITableViewCell UILabel Text in Swift throws error -



ios - Access to Custom UITableViewCell UILabel Text in Swift throws error -

i'm trying create custom uitableviewcell, including own uilabel inside.

i've created single view application swift, , added cocoa touch class .xib file, add together uilabel , create outlet in new file(customcell class).

in main storyboard i've added uitableview, , initialize info (implement uitableviewdatasource , uitableviewdelegate).

when i'm trying access text property in uilabel, fail error:

fatal error: can't disclose optional.none

this customcell class:

import uikit class customcell: uitableviewcell { @iboutlet var mylbl: uilabel override func awakefromnib() { super.awakefromnib() } func setvalue(value: string){ mylbl.text = value // thread 1: exc_bad_instruction (code=exc_i386_invop, subcode=0x0) } override func setselected(selected: bool, animated: bool) { super.setselected(selected, animated: animated) } }

update: here code creates cells in main viewcontroller:

func tableview(tableview: uitableview!, cellforrowatindexpath indexpath: nsindexpath!) -> uitableviewcell!{ var cell = customcell(style: uitableviewcellstyle.default, reuseidentifier: "default") cell.setvalue(stringarray[indexpath.row]) homecoming cell; }

you getting error because mylbl nil.

that because not creating cell using nib. using init style. should do, register nib table view , utilize deque on tableview:

override func viewdidload() { var cellnib = uinib(nibname:"anibname", bundle: nil) self.tableview.registernib(cellnib, forcellreuseidentifier: "default") } func tableview(tableview: uitableview!, cellforrowatindexpath indexpath: nsindexpath!) -> uitableviewcell!{ var cell = tableview.dequeuereusablecellwithidentifier("default", forindexpath: indexpath) cutomcell cell.setvalue(stringarray[indexpath.row]) homecoming cell; }

if, however, creating template cells in nib view controller, don't have register seperate nib cell still need deque cell above.

ios xcode uitableview swift

No comments:

Post a Comment