添加虚线底部边框表视图单元格的文本中随机重叠的iOS

问题描述 投票:0回答:1

我使用下面的代码,以虚线定制底部添加边框的tableview细胞。现在与内容重叠随机。有时,是没有得到显示的边框。

class AppTableCell: UITableViewCell {
    var shapeLayer: CAShapeLayer?
    var isBorderAdded = false

    func isBottomBorderAdded() -> Bool {
        return isBorderAdded
    }

    func getBottomBorderShapeLayer() -> CAShapeLayer {
        return self.shapeLayer!
    }

    func setBottomBorderShapedLayer(_ layer: CAShapeLayer) {
        self.shapeLayer = layer
    }
}

在从上述类延伸的tableview细胞并调用func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell方法的下面功能。

func addDashedBottomBorder(to cell: AppTableCell) {
    let color = UIColor.init(red: 191/255, green: 191/255, blue: 191/255, alpha: 1.0).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 = CAShapeLayerLineJoin.round
    shapeLayer.lineDashPhase = 3.0
    shapeLayer.lineDashPattern = [9,6]
    shapeLayer.path = UIBezierPath(roundedRect: CGRect(x: 0, y: shapeRect.height, width: shapeRect.width, height: 0), cornerRadius: 0).cgPath
    if (cell.isBorderAdded) {
        cell.shapeLayer!.removeFromSuperlayer()
    }
    cell.shapeLayer = shapeLayer
    cell.isBorderAdded = true
    cell.layer.addSublayer(shapeLayer)
}

border overlap

如何在每一个细胞正常结束时显示虚线底部边框?

ios swift uitableview border
1个回答
0
投票

你要添加在你的细胞的层子层用一个固定的位置:

shapeLayer.position = CGPoint(x: frameSize.width/2, y: frameSize.height)
[...]
cell.layer.addSublayer(shapeLayer)

所以也没有坚持你的电池的底部。

你可以在每次细胞的高度变化更新该层的位置,但是从性能和简单一点我的建议是使用自动布局。

在你的底部添加一个子视图,添加约束,使之坚持,和(例如,如果它在IB设计awakeFromNib)内只添加一次dashedLayer。

© www.soinside.com 2019 - 2024. All rights reserved.