ios - 仅在 tableview 单元格中制作左下角

标签 ios iphone swift uitableview

我有一个问题。
我想像这样制作关于角落的 View ,但我不知道该怎么做:

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中使用corner函数

//var labelContent:UILabel

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


} 


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

    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
}

更新:我听从刺猬先生的建议:
当我向上滚动时,单元格 View 消失。

 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
}

最佳答案

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

将第一行改为:

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

然后尝试。第二件事——你确定你需要更新标签的角而不是包含它的内容 View 吗?

关于ios - 仅在 tableview 单元格中制作左下角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43665339/

相关文章:

ios - iOS 中 didReceiveRemoteNotification 的两个委托(delegate)方法有什么区别?

ios - 设备是否需要在配置门户中注册才能在其上安装企业应用程序?

iphone - 如何在屏幕解锁后重新启动动画

ios - 创建一个将迭代图像数组的 UIImageView

iphone - SimpleAudioEngine,播放.caf文件

iphone - 在不使用 UITabBarController 创建项目的情况下使用 UITabBar

iphone - xamarin studio (Monotouch) 中出现错误 MT2002

ios - libc++abi.dylib : terminating with uncaught exception of type NSException on iOS 7. 1

ios - 如果应用程序未在 Swift iOS 中运行,如何读取一个信号通知

ios - Firebase iOS 用户身份验证 : avoiding getting logged out of app