2016-10-28 68 views
0

我想畫虛線下邊框到UITableViewCells用下面的代碼:企圖拉攏虛線邊框的UITableViewCell

func addDashedBottomBorder(to cell: UITableViewCell) { 
    let width = CGFloat(2.0) 
    let dashedBorderLayer: CAShapeLayer = CAShapeLayer() 
    let frameSize = cell.frame.size 
    let shapeRect = CGRect(x: 0, y: frameSize.height, width: frameSize.width*2, height: 1) 

    dashedBorderLayer.bounds = shapeRect 
    dashedBorderLayer.position = CGPoint(x: 0, y: frameSize.height) 
    dashedBorderLayer.strokeColor = UIColor.lightGray.cgColor 
    dashedBorderLayer.lineWidth = width 
    dashedBorderLayer.lineDashPattern = [9, 6] 
    dashedBorderLayer.path = UIBezierPath(roundedRect: shapeRect, cornerRadius: 5).cgPath 

    cell.layer.addSublayer(dashedBorderLayer) 
} 

但是,我得到我的虛線CAN背後一個奇怪的實線在這裏看到:http://imgur.com/6kR9PgZ

我已經在viewDidLoad

任何爲什麼我收到實線背後的虛線一個想法設置tableView.separatorColor = UIColor.clear

+0

你檢查了答案 –

回答

2

試試這個

func addDashedBottomBorder(to cell: UITableViewCell) { 

    let color = UIColor.lightGray.cgColor 

    let shapeLayer:CAShapeLayer = CAShapeLayer() 
    let frameSize = cell.frame.size 
    let shapeRect = CGRect(x: 0, y: 0, width: frameSize.width, height: 0) 

    shapeLayer.bounds = shapeRect 
    shapeLayer.position = CGPoint(x: frameSize.width/2, y: frameSize.height) 
    shapeLayer.fillColor = UIColor.clear.cgColor 
    shapeLayer.strokeColor = color 
    shapeLayer.lineWidth = 2.0 
    shapeLayer.lineJoin = kCALineJoinRound 
    shapeLayer.lineDashPattern = [9,6] 
    shapeLayer.path = UIBezierPath(roundedRect: CGRect(x: 0, y: shapeRect.height, width: shapeRect.width, height: 0), cornerRadius: 0).cgPath 

    cell.layer.addSublayer(shapeLayer) 
} 

輸出

enter image description here

1

Objective-C的

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 

在斯威夫特

tableView.separatorStyle = .None 
+0

它沒有任何區別。問題在於層邏輯 –

1

你與你的donted線顯示該生產線是默認TableViewCell分隔符,你可以從界面生成器直接刪除它,而不是寫任何的碼。

在界面構建器中選擇TableView並在Attributes Insepector中設置Separator屬性爲None

enter image description here

2

,如果你有寫

tableView.separatorS tyle =。無

仍然,喲正在獲得一個實線下方的模式,然後100%它不是tableview分隔符。這是別的東西

檢查您的表背景顏色,當您清除分隔符顏色時,一個 小空間保持單元格之間。如果您將單元格背景色 與表格背景顏色的顏色不同,則可以看到類似於 分隔符。