Swift Tableview单元格阴影无法正确渲染

Swift Tableview单元格阴影无法正确渲染,swift,uitableview,shadow,dropshadow,Swift,Uitableview,Shadow,Dropshadow,我已经能够使用下面的代码将阴影添加到我的tableview单元格的底部,但是它似乎在之后消失了 我向下滚动 cell.layer.shadowColor = UIColor.blackColor().CGColor cell.layer.shadowOffset = CGSizeMake(0,2) cell.layer.shadowRadius = 3 cell.layer.shadowOpacity = 0.5 let shadowFrame: CGRect = cell.layer.boun

我已经能够使用下面的代码将阴影添加到我的tableview单元格的底部,但是它似乎在之后消失了 我向下滚动

cell.layer.shadowColor = UIColor.blackColor().CGColor
cell.layer.shadowOffset = CGSizeMake(0,2)
cell.layer.shadowRadius = 3
cell.layer.shadowOpacity = 0.5
let shadowFrame: CGRect = cell.layer.bounds
let shadowPath: CGPathRef = UIBezierPath(rect: shadowFrame).CGPath
cell.layer.shadowPath = shadowPath
cell.layer.masksToBounds = false

问题与阴影偏移有关,我已通过以下方法纠正了该问题:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    cell.layer.backgroundColor = UIColor.clearColor().CGColor
    cell.layer.shadowColor = UIColor.blackColor().CGColor
    cell.layer.shadowOffset = CGSizeZero
    cell.layer.shadowRadius = 4
    cell.layer.shadowOpacity = 0.5
    cell.layer.shadowPath = UIBezierPath(rect: cell.bounds).CGPath
    cell.layer.masksToBounds = false

}

你把这个代码放在哪里?在UITableViewCell的子类中,或在cellForRowAtIndexPath中,或在willDisplayCell中?