在tableview单元格中只做左下角

时间:2017-04-27 18:23:44

标签: ios iphone swift uitableview

我有问题。
我想像这样对角落进行观察,但我不知道如何做到这一点:

i want to make this corner view

我也遵循了Stackoverflow问题question url

extension UIView {
func roundCorners(corners:UIRectCorner, radius: CGFloat) {
let mask = CAShapeLayer()
mask.path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
layer.mask = mask
}

所以我在layoutsubview中使用了角落功能

//var labelContent:UILabel

override func layoutSubviews() {
    super.layoutSubviews()
    labelContent.roundCorners(corners: [.bottomLeft,.topLeft,.topRight], radius: 10)


} 


它工作但当我向上滚动时,单元格视图消失 所以,我使用第二个功能 但它也不起作用

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    let cell: ChatContentTableViewCell = ChatContentTableViewCell(style: .default, reuseIdentifier: nil)

    let maskPathTop = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.bottomRight], cornerRadii: CGSize(width: 0, height: 0))
    let shapeLayerTop = CAShapeLayer()
    shapeLayerTop.frame = cell.labelContent.bounds
    shapeLayerTop.path = maskPathTop.cgPath
}

更新:我跟随Hedgehog先生的建议:
当我向上滚动时,单元格视图消失。

 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    guard let cell = cell as? ChatContentTableViewCell else { return }

    let maskPathTop = UIBezierPath(roundedRect: cell.contentView.bounds, byRoundingCorners: [.bottomRight], cornerRadii: CGSize(width: 0, height: 0))
    let shapeLayerTop = CAShapeLayer()
    shapeLayerTop.frame = cell.contentView.bounds
    shapeLayerTop.path = maskPathTop.cgPath
}

1 个答案:

答案 0 :(得分:1)

第二个解决方案不起作用,因为您正在实例化单元格的新实例,而不是使用回调提供的实例。

将第一行更改为:

guard let cell = cell as? ChatContentTableViewCell else { return }

然后试试。第二件事 - 您确定需要更新标签的角落而不是内容视图吗?

相关问题