如何在静态表swift Xcode 6中设置单元格的背景颜色

时间:2015-04-10 19:23:54

标签: static ios8 tableview xcode6.1

我有静态表格,我为tableview -> tableView.backgroundColor = UIColor.redColor()设置背景颜色,当我在静态表格中有3行时,表格为彩色,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
}

6 个答案:

答案 0 :(得分:1)

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

cell.backgroundcolor = UIColor.clearcolor()

干杯 萨姆

答案 1 :(得分:1)

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

答案 2 :(得分:0)

请不要在这种情况下使用cellForRowAtIndexPath方法,因为正如您所说,您正在使用静态tableview。

现在来解决方案。今天我刚刚遇到了同样的问题,这个场景与你的几乎相同。所以我的解决方案是 -

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 :(得分:0)

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

cell.contentView.backgroundColor = UIColor.greenColor()

答案 4 :(得分:0)

快速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
    }
}

答案 5 :(得分:0)

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

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包含在内,以使其更具动态性。

祝你好运!