2017-04-18 127 views
2

我想添加陰影在tableviewcell與4節(1 tableView與4 tableViewCell),最後一個單元格我想添加三邊(左,右,底部)陰影,但在另一個單元格,但最後一個單元格,我只是想在左側(左側,右側)添加陰影。我用singleton和擴展UIView編寫這段代碼。 此爲最後一個單元格:如何在tableviewcell swift 3添加陰影?

func dropShadowAtBottom() { 

    self.layer.masksToBounds = false 
    self.layer.shadowColor = UIColor.black.cgColor 
    self.layer.shadowOpacity = 0.2 
    self.layer.opacity = 0.3 
    self.layer.shadowOffset = CGSize(width: 0, height: 2) 
    self.layer.shadowRadius = 2 
    self.clipsToBounds = false 

} 

這對於除最後一個小區中其他小區:

func dropShadowAtLeftAndRight() { 

    self.layer.masksToBounds = false 
    self.layer.shadowColor = UIColor.black.cgColor 
    self.layer.shadowOpacity = 0.5 
    self.layer.shadowOffset = CGSize(width: 0, height: 0) 
    self.layer.shadowRadius = 2 
    let shadowRect: CGRect = self.layer.bounds.insetBy(dx: 0, dy: 4); // inset top/bottom 
    self.layer.shadowPath = UIBezierPath(rect: shadowRect).cgPath 

    self.layer.rasterizationScale = UIScreen.main.scale 
    self.layer.shouldRasterize = true 

} 

我把這個代碼在cellForRow:

if(indexPath.row == currentOrderHistory.listOrderItems.count - 1){ 
      cell.bodyView.dropShadowAtBottomOnly() 

     }else{ 
      cell.bodyView.dropShadowAtLeftAndRight() 
     } 
     return cell 

現在這個代碼的工作,但仍不完美。細胞之間有白色空間。

picture

我只想讓他們連接。

+2

如果您發佈想要實現的圖像 –

+0

試圖分享您想要實現的內容 –

+0

我會添加圖片。請klik的「圖片」文字 –

回答

0

您是否嘗試從IB中刪除分隔符?

enter image description here

設置的分離

或另一種方式,設置分隔符顏色半透明

self.tableView.separatorColor = UIColor.clear 
+0

謝謝,但它沒有任何變化 –

0

我想你在這一行面臨的問題dropShadowAtLeftAndRight()

let shadowRect: CGRect = self.layer.bounds.insetBy(dx: 0, dy: 4); 

它會返回rect看起來像這樣(0.0, 4.0, 375.0, 36.0)在我的情況。在上面4PX是白色的空間,底部也4PX空白

所以,你的左邊和右邊的陰影從400開始,高度36

來解決這個問題,你需要這樣設置

let shadowRect: CGRect = self.layer.bounds.insetBy(dx: 0, dy: 0); 
+0

是的,但如果我改變dy爲0,所有單元格將有底部的影子 –

+0

那麼你只能使用「dropShadowAtBottom」方法,並在你的單元格左右添加兩個視圖給左和右灑下。 –