How to place CALayer under UITableViewCell. Swift



  • I need to make a shadow under specific circumstances. UITableViewCellI did it, but I have a problem when I do. scroll down there, it's all right, but when I do scroll Upwards, the cell that has a shadow, this shadow is visible not under the cell, but above it. How can I fix this?

            func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier, forIndexPath: indexPath) as! LifelineLeaderboardTableViewCell
    
        // Configure the cell...
        let lifelineRecentModel = users[indexPath.row]
    
        cell.clipsToBounds = false
        if let currentUserID = DBHelper.instance.mainUserId {
            if lifelineRecentModel.user.id == currentUserID {
                cell.setupUserNumberLabelTextColor(true)
                cell.showBlueLineView(true)
    

    // cell.showShadow(true)
    let shadowView = UIView(frame: cell.bounds)

                let shadowFrame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: cell.bounds.width, height: 90))
                let shadowPath = UIBezierPath(rect: shadowFrame).CGPath
                let shadow = CAGradientLayer()
    
                shadow.shadowOpacity = 0.25
                shadow.shadowColor = UIColor.blackColor().CGColor
                shadow.shadowPath = shadowPath
                shadowView.layer.insertSublayer(shadow, atIndex: 0)
                cell.contentView.addSubview(shadowView)
            } else {
                cell.setupUserNumberLabelTextColor(false)
                cell.showBlueLineView(false)
                cell.showShadow(false)
            }
        } else {
            cell.setupUserNumberLabelTextColor(false)
            cell.showBlueLineView(false)
            cell.showShadow(false)
        } 
    
        return cell
    }
    



  • You put a shadowView on the top of all the bows every time. Replace the method addSubview in line cell.contentView.addSubview(shadowView)by method insertSubview:shadowView atIndex:0 or insertSubview:shadowView belowSubview: <cell.firstLabel>;




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2