UncleJunior
UncleJunior

Reputation: 241

Adding shadow to tableview cell cause laggy scroll and duplicate shadow

I have added white space around my tableview cell and every time I scroll this shadows getting bigger and bigger when I scroll, and its get lag when I scroll for the second and third time with the bigger shadow

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
}

Upvotes: 0

Views: 1197

Answers (3)

Casper Zandbergen
Casper Zandbergen

Reputation: 3687

You keep adding shadow views on top of each other without ever removing them. If all your cells will need the shadow you can just add a tag and check if a view with that tag already exists like so:

whiteRoundedView.tag = 12345

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

If some cells have the shadow but some cells don't you could do it like this:

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

Alternatively like mentioned in the comments of the question you would add it to your custom cell class:

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

Upvotes: 1

Torongo
Torongo

Reputation: 1091

You are adding whiteRoundedView every time. If you are using a storyboard or Nib for designing your Cell UI then you can add whiteRoundedView there Or you can add a tag in whiteRoundedView and check if there are already any view added with that tag before adding the whiteRoundedView as subview.

Upvotes: 0

Jaydeep Vyas
Jaydeep Vyas

Reputation: 4470

Just put code inside the 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)

        }


    }

Upvotes: 4

Related Questions