ios - How can I change text color of certain cells in UITableView? -
so, i'm making app in xcode 6 (swift 1). i'm trying make cells' text colors green, , others' red, however, i'm not successful. here's code:
import foundation import uikit class viewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource { @iboutlet var tableview: uitableview! var items: [string] = ["green", "red"] override func viewdidload() { super.viewdidload() self.tableview.registerclass(uitableviewcell.self, forcellreuseidentifier: "cell") } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return self.items.count; } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var cell:uitableviewcell = self.tableview.dequeuereusablecellwithidentifier("cell") uitableviewcell cell.textlabel?.text = self.items[indexpath.row] return cell } func tableview(tableview: uitableview!, didselectrowatindexpath indexpath: nsindexpath!) { println("you selected cell #\(indexpath.row)!") } }
and here's code changing universal text color:
cell.textlabel.textcolor = [uicolor greencolor];
so, example, how can make "green" cell green, , "red" cell red? thanks!
after var items
declaration, add this:
let itemcolors = [uicolor.greencolor(), uicolor.redcolor()]
in tableview(_:cellforrowatindexpath:)
, add before return statement:
cell.textlabel?.textcolor = itemcolors[indexpath.row]
Comments
Post a Comment