简体   繁体   English

如何更改tableview swift中文本的颜色?

[英]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. 我已经在swift中创建了一个tableview,我正在定制外观以使它看起来更漂亮。 I'd like to change the appearance to have a clear background and white text color. 我想改变外观以获得清晰的背景和白色文字颜色。 Tableview function: Tableview功能:

  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 如何在swift中的tableView部分更改文本颜色

https://www.natashatherobot.com/ios-change-uitableviewcell-selection-color-app-wide/ https://www.natashatherobot.com/ios-change-uitableviewcell-selection-color-app-wide/

Any help would be greatly appreciated! 任何帮助将不胜感激! Thanks 谢谢

You need to change the color of text using the "cell" object you created. 您需要使用您创建的“单元格”对象更改文本的颜色。 So it should be like this 所以它应该是这样的

cell.textLabel?.textColor = UIColor.cyanColor()

Change it to any color of your desire 将它改变成你想要的任何颜色

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. 首先,我不认为使用appearance()来全局更改所有单元格是明智的,但如果你这样做,你应该如你的链接所说,在AppDelegate调用它,而不是每次加载单元格时。

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. 或者,正如@Umair所提到的,您可以简单地将该调用更改为不是全局的,并直接更改颜色。

Just change in your table view code, like this: 只需更改表视图代码,如下所示:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Categoria")
    cell?.textLabel?.text = categoris[indexPath.row]
    cell?.textLabel?.textColor = UIColor.white // <- Changed color here
    return cell!
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM