2016-10-28 67 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分隔符。这是别的东西

检查您的表背景颜色,当您清除分隔符颜色时,一个 小空间保持单元格之间。如果您将单元格背景色 与表格背景颜色的颜色不同,则可以看到类似于 分隔符。