繁体   English   中英

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

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

我已经在swift中创建了一个tableview,我正在定制外观以使它看起来更漂亮。 我想改变外观以获得清晰的背景和白色文字颜色。 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;
}

所以第二部分与透明细胞一起工作,但第一部分不是出于某种原因。 我在网上查看了一些来源,但似乎无法正确使用这个:

如何在swift中的tableView部分更改文本颜色

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

任何帮助将不胜感激! 谢谢

您需要使用您创建的“单元格”对象更改文本的颜色。 所以它应该是这样的

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

将它改变成你想要的任何颜色

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

其次,您可以直接在故事板中自定义许多这些功能(因为您使用的是可重复使用的单元格,您真的应该这样做) - 找到您的单元格并更改其中的文本颜色。

或者,正如@Umair所提到的,您可以简单地将该调用更改为不是全局的,并直接更改颜色。

只需更改表视图代码,如下所示:

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