iOS:单元格背景更改时 UITableViewCell 的侧面显示不同的颜色(Xcode 8,Swift 3)

iOS: Sides of UITableViewCell showing a different color when cell background is changed (Xcode 8, Swift 3)

在实现 UITableView 时,我添加了背景图像并将 table 视图设置为透明。但是,即使我将 table 视图单元格更改为不同的颜色(以便更好地查看文本),table 的侧面仍然清晰,如图所示:Table View Cell .

我为获取 table 视图的内部部分而添加的代码:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.backgroundColor = UIColor(white: 1, alpha: 0.25)
        cell.accessoryView?.backgroundColor = UIColor(white: 1, alpha: 0.25)
        cell.contentView.backgroundColor = UIColor(white: 1, alpha: 0.25)
        cell.tintColor = UIColor(white: 1, alpha: 0.25)
}

并且在 viewWillAppear 中:

self.tableView.backgroundColor = UIColor(white: 1, alpha: 0.25)

我不确定为什么侧面显示的颜色不同。我以为它与附件按钮有关,但是当试图改变它时,没有任何改变。

contentView 没有覆盖整个单元格。尝试将其背景颜色设置为清晰颜色。然后只有单元格的背景视图会显示出来。

你的大部分代码都是纯粹的浪费;这与此事无关。关键行是:

cell.backgroundColor = UIColor(white: 1, alpha: 0.25)
cell.contentView.backgroundColor = UIColor(white: 1, alpha: 0.25)

这样做很愚蠢,因为您要用另一种背景颜色(位于单元格前面的内容视图的背景颜色)覆盖单元格的半透明背景色。

当你遇到这样的问题时,通过制作一个更简单、更清晰的测试用例来自己解决。例如:

    cell.backgroundColor = .red
    cell.contentView.backgroundColor = .green
    cell.accessoryType = .checkmark

这使得发生的事情一目了然:绿色内容视图部分遮挡了红色单元格。您的代码做的事情完全相同。