最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Swift中设置UITableView的cell的text的背景色不起效果

Swift crifan 2983浏览 0评论

[记录]Swift实现类似于微信的聊天对话框窗口

期间,用代码去设置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:

但是不起效果:

uitableview cell text background still none

 

后来发现好像是:

点击后,才出现背景色

after click background changed

 

搜:

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()

即可生效:

change cell background color can work

转载请注明:在路上 » [已解决]Swift中设置UITableView的cell的text的背景色不起效果

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.173 seconds, using 22.12MB memory