【问题标题】:Cell image view cornerRadius in swiftswift中的单元格图像视图cornerRadius
【发布时间】:2017-10-30 15:24:46
【问题描述】:

我最好在哪里设置cornerRadius 属性?

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 1/6 * tableView.bounds.width
}

我已经在单元格的 awakeFromNib 中尝试过,但它似乎还没有合适的高度。

【问题讨论】:

标签: ios swift uitableview


【解决方案1】:

如果图像视图大小灵活且圆角半径与图像大小相关联,我建议您在单元格的layoutSubviews() 方法中设置圆角半径。

你可以设置如下:

override func layoutSubviews() {
    super.layoutSubviews()
    imageView.layer.cornerRadius = imageView.frame.size.width/2.0
}

【讨论】:

  • 即使第一次调用这个,似乎也无法得到正确的imageView.frame。
【解决方案2】:

好的警告 - 所有未经测试的代码

如果你认为最了解如何绘制单元格的对象就是单元格本身......

创建 Cell 的子类

class MyUITableViewCell: UITableViewCell

现在,有很多方法可以给这个细胞蒙皮。我的偏好是:

为单元格中您希望更改的所有小事情添加一个方法

func configure () {

    self.layer.cornerRadius = 5.0 // for instance

}

什么时候调用configure?

可以说你可以在 draw() 中调用,但我喜欢

通过委托调用 configure()

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

    if cell.responds(to: #selector(MyUITableViewCell.configure()) {

        cell.configure()

    }
}

【讨论】:

    猜你喜欢
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    相关资源
    最近更新 更多