【问题标题】:How to change the color of text in a tableview swift?如何快速更改表格视图中文本的颜色?
【发布时间】:2016-05-30 05:32:38
【问题描述】:

我在 swift 中创建了一个 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;
}

所以第二部分适用于透明细胞,但第一部分由于某种原因不能。我在网上查看了一些资源,但似乎无法正确理解:

How to change text color in tableView section in swift

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

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

【问题讨论】:

    标签: swift uitableview


    【解决方案1】:

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

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

    把它改成你想要的任何颜色

    【讨论】:

      【解决方案2】:

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

      其次,您可以直接在情节提要中自定义其中的许多功能(而且由于您使用的是可重复使用的单元格,因此您确实应该这样做) - 找到您的单元格并在那里更改文本颜色。

      或者,正如@Umair 所述,您可以简单地将该调用更改为非全局调用,然后直接更改颜色。

      【讨论】:

        【解决方案3】:

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

        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!
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-01-15
          • 2012-03-26
          • 1970-01-01
          • 1970-01-01
          • 2014-06-29
          • 1970-01-01
          • 2022-11-04
          • 2015-10-07
          相关资源
          最近更新 更多