2016-07-12 50 views
0

我需要在特定的UITableViewCell底部製作一個陰影。我做了它,但當我滾動到底部時,它工作正常,當我滾動到頂部時,陰影位置出錯,它出現在UITableViewCell的頂部。我嘗試了幾種方法,但它不適合我。我讀了這個question和這個question我該如何解決它?CAGradientLayer在滾動到UITableView頂部後位置錯誤。 Swift

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 
    } 

而且我想我下面的函數

func showShadow(bool: Bool) { 
    let shadowFrame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: bounds.width, height: bounds.height + 10)) 
    let shadowPath = UIBezierPath(rect: shadowFrame).CGPath 

    layer.shadowOpacity = 0.25 
    layer.shadowColor = UIColor.blackColor().CGColor 
    layer.shadowPath = shadowPath 
    clipsToBounds = !bool 
} 

我也試了一下

 cell.clipsToBounds = false 
     if let currentUserID = DBHelper.instance.mainUserId { 
      if lifelineRecentModel.user.id == currentUserID { 
       cell.setupUserNumberLabelTextColor(true) 
       cell.showBlueLineView(true) 
//    cell.showShadow(true) 
       cell.layer.shadowPath = UIBezierPath(rect: cell.bounds).CGPath 
       cell.layer.shadowOpacity = 0.5 
       cell.layer.shadowOffset = CGSize(width: 0, height: 10) 

      } else { 
       cell.setupUserNumberLabelTextColor(false) 
       cell.showBlueLineView(false) 
//    cell.showShadow(false) 
       cell.layer.shadowOpacity = 0 

      } 
     } else { 
      cell.setupUserNumberLabelTextColor(false) 
      cell.showBlueLineView(false) 
//   cell.showShadow(false) 
      cell.layer.shadowOpacity = 0 
     } 

回答

1

我想你應該從哪些不能包含陰影細胞「刪除」的影子。像這樣

if let currentUserID = DBHelper.instance.mainUserId { 
    .......... 
} else { 
    ....... 
    shadow.shadowColor = UIColor.clearColor().CGColor 
} 

您可以添加屏幕截圖'壞'單元嗎?

+0

我試過了,但它對我不起作用 – Alexander

+0

可能是你應該刪除'shadowView'?例如,使用屬性'標籤'爲陰影視圖,以刪除它。 – RrrangeTop

相關問題