期间,用代码去设置cell的背景色:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { print("cellForRowAtIndexPath indexPath = \(indexPath)") let cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: TABLE_CELL_ID_CHAT) cell.textLabel?.font = UIFont.boldSystemFontOfSize(14) cell.textLabel?.textAlignment = NSTextAlignment.Center switch indexPath.row { case 0: cell.textLabel?.text = "您已添加了吴滔,现在可以开始聊天了。" cell.textLabel?.backgroundColor = UIColor.lightGrayColor() break case 1: cell.textLabel?.text = "如果陌生人主动添加你为朋友,请谨慎核实对方身份。" cell.textLabel?.backgroundColor = UIColor.lightTextColor() break; case 2:
但是不起效果:
后来发现好像是:
点击后,才出现背景色
搜:
swift uitableviewcell text background color not work
参考:
ios – UITableView set background color – Stack Overflow
把:
cell.textLabel?.backgroundColor = UIColor.lightGrayColor()
换为:
cell.backgroundColor = UIColor.lightGrayColor() cell.backgroundColor = UIColor.darkGrayColor()
即可生效: