ThirtyKnots ThirtyKnots - 2 years ago 169
Swift Question

How to change the color of text in a tableview swift?

I've created a tableview in swift and I'm customising appearance to make it look nicer. I'd like to change the appearance to have a clear background and white text color. Tableview function:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell:UITableViewCell = self.myTableView
.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
let score = scores[indexPath.row]
cell.textLabel!.text = String(score.gameScore!)
UITableViewCell.appearance().textLabel?.textColor = UIColor.whiteColor();
UITableViewCell.appearance().backgroundColor = UIColor.clearColor();

return cell;

So the second part works with the clear cells but but the first part doesn't for some reason. I checked out some sources online but can't seem to get this one right:

How to change text color in tableView section in swift

Any help would be greatly appreciated! Thanks

Answer Source

First, I don't think it's wise to use appearance() to globally alter all your cells, but if you do it, you should be, as your link says, calling it in your AppDelegate, not each time you load a cell.

Second, you can customize many of these features directly in storyboard (and since you're using a reusable cell, you really should) - go find your cell and change the text color there.

Or, as mentioned by @Umair, you can simply change that call to not be global, and change the color directly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download