当我在表格视图中向上或向下滚动时,当单元格在视图外部时,它看起来像clipsToBounds或masksToBounds重置,因为阴影没有与其他单元格重叠,所以您看到它们之间的线。 检查第一张图片的第一个单元格和第二张图片的第一个单元格,在其中将单元格滚动到视图之外。
这是我在cellForRowAt中单元格样式的代码:
cell.itemBackground.layer.shadowColor = UIColor.black.cgColor
cell.itemBackground.layer.shadowOpacity = 0.10
cell.itemBackground.layer.shadowOffset = CGSize(width: 0, height: 10)
cell.itemBackground.clipsToBounds = false
cell.itemBackground.layer.masksToBounds = false
cell.itemBackground.layer.shadowRadius = 15
cell.itemBackground.layer.shadowPath = UIBezierPath(rect: cell.itemBackground.bounds).cgPath
cell.itemBackground.layer.shouldRasterize = true
cell.itemBackground.layer.rasterizationScale = UIScreen.main.scale
cell.clipsToBounds = false
cell.layer.masksToBounds = false
cell.selectionStyle = .none
答案 0 :(得分:0)
添加阴影的最佳方法是在单元格类中,因此它只会加载一次。
UITableViewCell
是awakeFromNib
中的类,而不是cellForRow
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
itemBackground.layer.shadowColor = UIColor.black.cgColor
itemBackground.layer.shadowOpacity = 0.10
itemBackground.layer.shadowOffset = CGSize(width: 0, height: 10)
itemBackground.clipsToBounds = false
itemBackground.layer.masksToBounds = false
itemBackground.layer.shadowRadius = 15
itemBackground.layer.shadowPath = UIBezierPath(rect: itemBackground.bounds).cgPath
itemBackground.layer.shouldRasterize = true
itemBackground.layer.rasterizationScale = UIScreen.main.scale
clipsToBounds = false
layer.masksToBounds = false
selectionStyle = .none
}