ios - 无法以编程方式设置单元格容器 BG

标签 ios swift uitableview

我有一个像 BasicCell>ContentView>Container 这样的 UITableView 单元设置。当我在界面生成器中设置容器的背景时,我得到了我设置的所需的深灰色阴影。但是,当我以编程方式设置较暗的阴影时,在 cellForRowAtIndexPath 中,我得到了白色背景。

cell.container.backgroundColor = UIColor(red: 20, green: 20, blue: 20, alpha: 1.0)

因此,通过引入程序化颜色设置,它突然忽略了界面构建器颜色和程序化颜色。我已经阅读了几个主题并尝试过使用 willDisplay 单元格之类的东西:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    let myCell = cell as! CustomTableViewCell_F2
    myCell.backgroundColor = UIColor.clearColor()
    myCell.backgroundView = nil
    myCell.container.backgroundColor = UIColor(red: 20, green: 20, blue: 20, alpha: 1.0)
}

我也试过在 CustomTableCell 类中设置它。那没有用。发生了什么,我该如何解决?

最佳答案

改变

cell.container.backgroundColor = UIColor(red: 20, green: 20, blue: 20, alpha: 1.0)

let shade: CGFloat = 0.2
cell.container.backgroundColor = UIColor(white: shade, alpha: 1.0)

你的颜色UIColor(red: 20, green: 20, blue: 20, alpha: 1.0)实际上是白色。您应该提供 0.0 到 1.0 范围内的颜色分量值。

关于ios - 无法以编程方式设置单元格容器 BG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36842373/

相关文章:

ios - TableView 不显示详细 View

ios - 滚动时 UICollectionView 选择丢失

swift - 一个实体中的两个相同的 parent

ios - UITableView 禁用单元格选择之间的突出显示

iOS 在 Swift 中以编程方式添加约束

ios - 如何在不使用单例的情况下在多个 View Controller 之间传递数据?

ios - 如何从自定义 TableViewCell 切换到另一个 ViewController (Swift)

ios - UITextView textViewDidChangeSelection 被调用两次

ios - 如何在 swift 2 中更新多个核心数据属性

iPhone SDK - 另一个控件内的相对位置?