swift - 向 tableview 单元格添加阴影会导致滚动滞后和重复阴影

标签 swift uitableview swift3 shadowpath

我在我的 tableview 单元格周围添加了空白区域,每次我滚动时,这个阴影会越来越大,当我第二次和第三次滚动时,它会变得滞后

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
    cell.backgroundColor = UIColor.clear
    cell.contentView.backgroundColor = UIColor.clear

    let whiteRoundedView : UIView = UIView(frame: CGRect(x:10,y: 5,width: self.view.frame.size.width - 20,height: 117))

    whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 0.9])
    //whiteRoundedView.layer.masksToBounds = true
    whiteRoundedView.layer.cornerRadius = 5.0
    whiteRoundedView.layer.shadowOffset = CGSize(width: -1,height: 1)
    whiteRoundedView.layer.shadowOpacity = 0.2

    let shadowPath = UIBezierPath(rect: whiteRoundedView.layer.bounds)
    whiteRoundedView.layer.shouldRasterize = true
    whiteRoundedView.layer.shadowPath = shadowPath.cgPath
    cell.contentView.addSubview(whiteRoundedView)
    cell.contentView.sendSubview(toBack: whiteRoundedView)

    return cell
}

最佳答案

只需将代码放在 awakefrom nib 中

  class CustomCell: UITableViewCell {



        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
    self.backgroundColor = UIColor.clear
        self.contentView.backgroundColor = UIColor.clear

        let whiteRoundedView : UIView = UIView(frame: CGRect(x:10,y: 5,width: self.contentView.frame.size.width - 20,height: 117))

        whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 0.9])
        //whiteRoundedView.layer.masksToBounds = true
        whiteRoundedView.layer.cornerRadius = 5.0
        whiteRoundedView.layer.shadowOffset = CGSize(width: -1,height: 1)
        whiteRoundedView.layer.shadowOpacity = 0.2

        let shadowPath = UIBezierPath(rect: whiteRoundedView.layer.bounds)
        whiteRoundedView.layer.shouldRasterize = true
        whiteRoundedView.layer.shadowPath = shadowPath.cgPath
        self.contentView.addSubview(whiteRoundedView)
        self.contentView.sendSubview(toBack: whiteRoundedView)

        }


    }

关于swift - 向 tableview 单元格添加阴影会导致滚动滞后和重复阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45409426/

相关文章:

ios - Instagram/tags/\(hashtag)/media/recent 端点不返回分页?

ios - 使 tableview 单元格以 textView 文本大小扩展

ios - UITableViewCell 可重用性问题。修改一个细胞会影响其他细胞

swift - Swift 3.0 中如何区分类(确定它不是结构体或枚举)

ios - Swift:制作 AVAudioPlayer 的错误处理程序

ios - PodFile中 'GoogleCloudMessaging'和 'Google/CloudMessaging'之间的区别

ios - UITableViewCell 不水平裁剪内容

iphone - 导航 Controller 上的编辑按钮不进入编辑模式

ios - Swift:将 View 从堆栈 View 带到前面

swift - 在 Swift 中 NSToolbar 的上下文菜单中仅显示 "Customize Toolbar..."