[英]Cell image view cornerRadius in swift
Where would I best set the cornerRadius property? 我最好在哪里设置cornerRadius属性?
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 1/6 * tableView.bounds.width
}
I have tried it in the cell's awakeFromNib, but it doesn't yet seem to have the proper height. 我已经在单元的awakeFromNib中尝试过了,但是它似乎还没有合适的高度。
If the image view size if flexible and the corner radius is linked to the image size, I advise you to set the corner radius in the layoutSubviews()
method of your cell. 如果图像视图的大小(如果灵活)并且拐角半径与图像大小相关联,则建议您在单元格的
layoutSubviews()
方法中设置拐角半径。
You can set it as following: 您可以将其设置如下:
override func layoutSubviews() {
super.layoutSubviews()
imageView.layer.cornerRadius = imageView.frame.size.width/2.0
}
Okay warning - all untested code 好的警告-所有未经测试的代码
If you assume that the object which knows best how to draw a cell, is the cell itself.... 如果假设最了解如何绘制单元格的对象是单元格本身。
Create a subclass of the Cell 创建单元的子类
class MyUITableViewCell: UITableViewCell
Now, there are many ways to skin this cell. 现在,有很多方法可以使该单元格皮肤化。 My preference is to:
我的偏好是:
Add a method for all of the little things you wish to change in the cell 为您要在单元格中更改的所有小事情添加方法
func configure () {
self.layer.cornerRadius = 5.0 // for instance
}
When should you call configure? 您什么时候应该打电话给configure?
Arguably you could call in in draw() but I like to 可以说您可以在draw()中调用,但是我喜欢
Call configure() via the delegate 通过委托调用configure()
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if cell.responds(to: #selector(MyUITableViewCell.configure()) {
cell.configure()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.