【问题标题】:How to set background color of cells in static table swift Xcode 6如何在静态表swift Xcode 6中设置单元格的背景颜色
【发布时间】:2015-04-10 19:23:53
【问题描述】:

我有静态表,我为tableview -> tableView.backgroundColor = UIColor.redColor() 设置了背景颜色,当我有等时。静态表中有 3 行,然后是 tableview 彩色,3 行是白色如何修复此单元格的背景颜色?此代码不适用于静态 表 ->

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell


    cell.textLabel!.text = numbers[indexPath.row]
    cell.backgroundColor = UIColor.greenColor()

    return cell
}

【问题讨论】:

    标签: static ios8 tableview xcode6.1


    【解决方案1】:

    在静态表视图中,您不必实现 cellForRowAtIndexPath。所以我建议你实现以下代码:

    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
       // Change the color of all cells     
       cell.backgroundColor = UIColor.greenColor()
    }
    

    【讨论】:

      【解决方案2】:

      请不要在这种情况下使用cellForRowAtIndexPath 方法,因为正如您所说,您使用的是静态表格视图。

      现在来解决问题。今天刚遇到同样的问题,场景和你的差不多。

      override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        cell.backgroundColor = UIColor .grayColor()
      }
      

      我只想告诉你一件事,根据 Apple 的文档,所有单元格的默认颜色都是透明/白色。因此,如果您尝试通过 Interface Builder 或 cellForRowAtIndexPath 方法设置颜色,如果您使用的是 iOS 7 / iOS 8 / iOS 9,它将默认返回白色。因此,不要在 cellForRowAtIndexPath 方法中声明,而是使用上面的代码。它会正常工作的。

      谢谢

      希望这会有所帮助。

      【讨论】:

        【解决方案3】:

        首先确保每个单元格的背景颜色都设置为 UI 模式中的默认值。在检查员属性下。然后在代码集中

        cell.backgroundcolor = UIColor.clearcolor()
        

        干杯 山姆

        【讨论】:

          【解决方案4】:

          UITableViewCell.appearance().backgroundColor = UIColor.greenColor()

          【讨论】:

            【解决方案5】:

            您可以更改 contentView 背景,因为它是单元格的子视图:

            cell.contentView.backgroundColor = UIColor.greenColor()
            

            【讨论】:

              【解决方案6】:

              对于 Swift 4:

              override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
                  if _isAccountNotSet {
                      print("_isAccountNotSet\(_isAccountNotSet)")
                      cell.backgroundColor = .groupTableViewBackground
                  } else {
                      print("_isAccountNotSet\(_isAccountNotSet)")
                      cell.backgroundColor = .white
                  }
              }
              

              【讨论】:

                【解决方案7】:

                要更改预期行的背景颜色,请使用以下代码:

                func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
                    {
                        if indexPath.row == 2
                        {
                            cell.contentView.backgroundColor = UIColor.lightGray
                        }
                
                    }
                

                此代码更改 IndexPath 2(第三行)的背景颜色。您可以将 2 更改为您想要的任何数字,或将其设为数字​​列表,如下所示:

                if indexPath.row == 2 || indexPath.row == 7
                        {
                            cell.contentView.backgroundColor = UIColor.lightGray
                        }
                

                您也可以使用一个变量代替 2 以使其更具动态性。

                祝你好运!

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2019-10-21
                  • 1970-01-01
                  • 2012-06-06
                  • 1970-01-01
                  • 2015-06-02
                  • 2011-10-10
                  • 1970-01-01
                  • 2010-11-21
                  相关资源
                  最近更新 更多