Thursday, 15 September 2011

ios - Swift: Create UILabel in Table View Cell -



ios - Swift: Create UILabel in Table View Cell -

i have table view controller in next array declared:

var newdata = [string]()

in viewdidload function, have following:

newdata = ["1", "2", "3"]

i'm trying create uilabel object within of table cell. far, have next in cellforrowatindexpath function:

let cell = tableview.dequeuereusablecellwithidentifier("tablecell", forindexpath: indexpath) uitableviewcell //programmatically create label var newlabel = uilabel(frame: cgrectmake(280.0, 14.0, 300.0, 30.0)) newlabel.tag = 1 newlabel.font = uifont(name: "avenir", size: 17.0) newlabel.textcolor = uicolor.darkgraycolor() cell.addsubview(newlabel) cell.contentview.viewwithtag(1) = newdata[indexpath.row] //this error

but, next error: "cannot assign result of expression". in objective-c, have written as:

[(uilabel *)[cell.contentview viewwithtag:1] settext:[newdata objectatindex:indexpath.row]];

my question is: how write preceding line of code in swift? give thanks in advance responses.

the easiest , proper way assign text be, when create label assign text @ time

var newlabel = uilabel(frame: cgrectmake(280.0, 14.0, 300.0, 30.0)) newlabel.text = newdata[indexpath.row] newlabel.tag = 1 edit

even if want utilize viewwithtag method have utilize in way typecasting

var lbl : uilabel? = cell.contentview.viewwithtag(1) as? uilabel lbl?.text = newdata[indexpath.row]

ios arrays uitableview swift uilabel

No comments:

Post a Comment