Search code examples
iosswiftuitableviewcellshadow

table view cell shadow not coming


I need this but the first cell is having no shadow but the second cell gets such shadow not getting the shadow for table view cell. i have connected the content view of the table view cell with the cell's viewcontroller and did this:

import UIKit

class staydetailsTableViewCell: UITableViewCell {

    @IBOutlet weak var price: UILabel!
    @IBOutlet weak var Content: UIView!
    @IBOutlet weak var image1: UIImageView!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        Content.layer.masksToBounds = false
        Content.layer.cornerRadius = 5
        Content.layer.shadowColor = UIColor.red.cgColor
        Content.layer.shadowOffset = CGSize(width: 0, height: 20)
        Content.layer.shadowRadius = 20
        Content.layer.opacity = 1
        Content.layer.borderColor = UIColor.lightGray.cgColor
        Content.layer.borderWidth = 1//(except for this nothing else is woking)


    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)


        // Configure the view for the selected state
    }

}

Solution

  • Try using layoutSubviews here is a code snippet that you can make use of. There is absolutely no need to add view called contentView just to show shadow, every single UITableViewCell comes with a embedded contentView

    override func layoutSubviews() {
            super.layoutSubviews()
            self.layer.cornerRadius = 5
            self.contentView.layer.cornerRadius = 5
            let shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: radius)
            self.layer.masksToBounds = false
            self.layer.shadowColor = UIColor.red.cgColor
            self.layer.shadowOffset = CGSize(width: 0.5, height: 1)
            self.layer.shadowOpacity = 0.25
            self.layer.shadowPath = shadowPath.cgPath
        }