ios - Number cells for UITableView -
i trying number cell 0-x uitableview. using label , want labels text 0,1,2,3,4,...x.
//label cell let cellframe: cgrect = cgrectmake(0, 0, 90, 32) var celllabel = uilabel(frame: cellframe) celllabel.textalignment = .center celllabel.font = uifont.mdfont.regularfourteen celllabel.text = ("\(indexpath.row)")
i have tried approach , 0,1,2,3,4 when scroll prints 0,1,2,3,4 on top of other numbers well.
this whole code function
func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var cell: uitableviewcell = tableview.dequeuereusablecellwithidentifier("cell") as! uitableviewcell if cell == null { //label cell let cellframe: cgrect = cgrectmake(0, 0, 90, 32) var celllabel = uilabel(frame: cellframe) celllabel.textalignment = .center celllabel.font = uifont.mdfont.regularfourteen celllabel.text = ("\(indexpath.row)") cell.contentview.addsubview(celllabel) } //print("table data \(tabledata[5])") return cell }
i think there issue related reusable cell. kindly try below code. may you.
func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var cell: uitableviewcell = tableview.dequeuereusablecellwithidentifier("cell") as! uitableviewcell if cell == null { //label cell let cellframe: cgrect = cgrectmake(0, 0, 90, 32) var celllabel = uilabel(frame: cellframe) celllabel.textalignment = .center celllabel.font = uifont.mdfont.regularfourteen celllabel.text = ("\(indexpath.row)") cell.contentview.addsubview(celllabel) } else { obj : anyobject in cell.contentview.subviews { if let label = obj as? uilabel { label.text = ("\(indexpath.row)") } } } //print("table data \(tabledata[5])") return cell }