【发布时间】:2018-01-17 19:46:31
【问题描述】:
我想在 Swift 中的 UITableView 的每一行中创建一个渐变背景层。在 cellForRowAt 函数中,我有这段代码来绘制渐变层。
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? PlayerTableViewCell else { fatalError("The dequeued cell is not an instance of PlayerTableViewCell") }
// set background color for the cell
let newLayer = CAGradientLayer()
newLayer.colors = [UIColor.black.cgColor, UIColor.darkGray.cgColor ]
newLayer.frame = cell.frame
cell.layer.addSublayer(newLayer)
cell.layer.insertSublayer(newLayer, at: 0)
当我运行这段代码时,只有表格中的第一个条目有渐变层,其他单元格有默认背景。 我需要添加一些东西吗?
【问题讨论】:
-
我们的回答能否解决您的问题?
标签: swift uitableview gradient