0

我在表格视图单元格周围添加了空白区域,每次滚动时,这个阴影会越来越大,当我第二次和第三次滚动时,阴影会变大

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
}
4

3 回答 3

4

只需将代码放入 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)

        }


    }
于 2017-07-31T07:22:28.727 回答
1

您不断地在彼此之上添加阴影视图,而无需删除它们。如果您的所有单元格都需要阴影,您可以添加一个标签并检查具有该标签的视图是否已经存在,如下所示:

whiteRoundedView.tag = 12345

if cell.contentView.viewWithTag(12345) == nil {
    cell.contentView.addSubview(whiteRoundedView)
}

如果某些单元格有阴影但有些单元格没有,您可以这样做:

if let shadowView = cell.contentView.viewWithTag(12345) && noShadow {
    shadowView.removeFromSuperview
} else if !noShadow {
    cell.contentView.addSubview(whiteRoundedView)
}

或者,就像问题评论中提到的那样,您可以将其添加到您的自定义单元格类中:

override func awakeFromNib() {
    super.awakeFromNib()

    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.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)
}
于 2017-07-31T07:15:23.100 回答
0

您每次都添加 whiteRoundedView 。如果您使用情节提要或 Nib 来设计 Cell UI,那么您可以在那里添加 whiteRoundedView 或者您可以在 whiteRoundedView 中添加一个标签,并在将 whiteRoundedView 添加为子视图之前检查是否已经有任何视图添加了该标签。

于 2017-07-31T07:48:35.693 回答