尝试为UITableViewCell绘制虚线边框

我正在尝试使用以下代码绘制UITableViewCells的虚线底部边框:

 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) } 

但是,我的虚线背后有一条奇怪的实线,如下所示: http : //imgur.com/6kR9PgZ

我已经在viewDidLoad设置了tableView.separatorColor = UIColor.clear

任何想法为什么我在虚线之后得到那么坚实的线条?

尝试这个

 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) } 

产量

在此处输入图像描述

如果你有写作

tableView.separatorStyle = .None

尽管如此,哟在破旧模式下面得到一条实线然后100%它不是tableview分隔符。 这是另一回事

检查表背景颜色,清除分隔符颜色时,单元格之间仍留有一小块空间。 如果单元格背景颜色与表格背景颜色不同,则可以像分隔符一样显示。

Objective-C的

 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 

在斯威夫特

 tableView.separatorStyle = .None 

您使用捐赠行显示的行是默认的TableViewCell分隔符,您可以直接从界面构建器中删除它,而不是编写任何代码。

在界面构建器中,选择TableView并在Attributes Insepector Set Separator属性中选择None

在此处输入图像描述