Swift 中的 TableView 单元格视图阴影未正确出现

TableView Cell view shadow does not coming properly in Swift

我已将视图添加为表格视图单元格的背景,并为视图提供阴影。在这里,当我 运行 tableview 阴影没有正常出现时,一旦我向下和向上滚动,阴影就会正常出现。

我已经按照这个答案给影子了answer

代码:

  extension UIView {
  func dropShadow(scale: Bool = true) {
  layer.masksToBounds = false
  layer.shadowColor = UIColor.black.cgColor
  layer.shadowOpacity = 0.5
  layer.shadowOffset = CGSize(width: -1, height: 1)
  layer.shadowRadius = 1
  layer.shadowPath = UIBezierPath(rect: bounds).cgPath
  layer.shouldRasterize = true
  layer.rasterizationScale = scale ? UIScreen.main.scale : 1
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PlansTableViewCell

    cell.containerView.dropShadow()
    return cell
}

之前滚动阴影是这样的:

滚动后如下所示:

在 运行ning 之后(滚动之前)我还需要第二种图像类型的输出,这里帮我写代码。

问题在这里

layer.shadowPath = UIBezierPath(rect: bounds).cgPath

bounds当时不对,试试 从

调用它
override func layoutSubviews() {
   super.layoutSubviews()
   self.containerViewdropShadow()
}

cell.layoutIfNeeded()
cell.containerView.dropShadow()