【问题标题】:How to create attributed text in a UITableView?如何在 UITableView 中创建属性文本?
【发布时间】:2014-12-14 17:27:53
【问题描述】:

所以我有课:

class BoatTypeGame: UITableViewController

表格视图有 14 行。从第 1 行到第 7 行,我想制作文本 green 的颜色,而对于第 8-14 行,我想制作文本 red 的颜色。顺便说一句,每次加载视图时单元格不会有相同的文本;我使用arc4random_uniform() 方法随机化文本。谢谢!

【问题讨论】:

  • 在这种情况下您不需要使用属性字符串。因为您可以在cellForRowAtIndexPathtableViewWillDisplayCell 方法中更改标签颜色

标签: uitableview swift uicolor nsmutableattributedstring


【解决方案1】:

在您的 cellForRowAtIndexPath 甚至 tableViewWillDisplayCell 中,只需使用 switch 语句或 if 语句。

switch indexPath.row {
case 1...7:
    println("row 1-7")
    cell.textLabel?.textColor = UIColor.greenColor()
case 8...14:
    println("row 8-14")
    cell.textLabel?.textColor = UIColor.redColor()
default:
    break
}

【讨论】:

  • 注意:在 swift 中,您不必在包含至少一个表达式的 case 语句中使用 break
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-26
  • 2014-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-22
  • 1970-01-01
相关资源
最近更新 更多