I’m trying to achieve a Card View layout with UITableView in Swift UIKit. When I use the below code, i get what i want but every time the cell comes to the visible area the background and the shadow gets darker and darker.
<code>func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.contentView.backgroundColor = UIColor.clear
let whiteRoundedView : UIView = UIView(frame: CGRectMake(15, 15, self.view.frame.size.width - 30, 90))
whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 1.0])
whiteRoundedView.layer.masksToBounds = false
whiteRoundedView.layer.cornerRadius = 3.0
whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, 1)
whiteRoundedView.layer.shadowOpacity = 0.5
if !cell.contentView.subviews.contains(whiteRoundedView) {
cell.contentView.addSubview(whiteRoundedView) //<- AAA
}
cell.contentView.sendSubviewToBack(whiteRoundedView)
}
</code>
<code>func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.contentView.backgroundColor = UIColor.clear
let whiteRoundedView : UIView = UIView(frame: CGRectMake(15, 15, self.view.frame.size.width - 30, 90))
whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 1.0])
whiteRoundedView.layer.masksToBounds = false
whiteRoundedView.layer.cornerRadius = 3.0
whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, 1)
whiteRoundedView.layer.shadowOpacity = 0.5
if !cell.contentView.subviews.contains(whiteRoundedView) {
cell.contentView.addSubview(whiteRoundedView) //<- AAA
}
cell.contentView.sendSubviewToBack(whiteRoundedView)
}
</code>
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.contentView.backgroundColor = UIColor.clear
let whiteRoundedView : UIView = UIView(frame: CGRectMake(15, 15, self.view.frame.size.width - 30, 90))
whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 1.0])
whiteRoundedView.layer.masksToBounds = false
whiteRoundedView.layer.cornerRadius = 3.0
whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, 1)
whiteRoundedView.layer.shadowOpacity = 0.5
if !cell.contentView.subviews.contains(whiteRoundedView) {
cell.contentView.addSubview(whiteRoundedView) //<- AAA
}
cell.contentView.sendSubviewToBack(whiteRoundedView)
}
AAA seems to call every time no matter the check because every time it gets passed.