我有以下自定义UITableViewCell子类,它在imageView属性中呈现一个圆角图像:
import UIKit
class PicturizedTableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
override func layoutSubviews() {
super.layoutSubviews()
self.imageView?.contentMode = UIViewContentMode.Center
self.imageView?.frame = CGRectMake(10, 10, 40, 40)
self.imageView?.layer.cornerRadius = (self.imageView?.frame.width)! / 2
self.imageView?.layer.masksToBounds = true
self.imageView?.layer.backgroundColor = UIColor.init(colorLiteralRed: 0.8,
green: 0.8,
blue: 0.8,
alpha: 1.0).CGColor
}
}
在我的UITableViewDelegate
中,我使用此自定义单元格(我使用Alamofire
从Web服务获取图像):
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PicturizedTableViewCell
// Configure the cell...
cell.textLabel?.text = (self.patients[indexPath.row] as Patient).name
cell.imageView?.image = UIImage(named: "patient-white")
let _url = (self.patients[indexPath.row] as Patient).photoUrl
if _url != "" {
cell.imageView?.af_setImageWithURL(
NSURL(string: "https:\(_url)")!,
imageTransition: .CrossDissolve(0.5))
}
return cell
}
我已在故事板中为我的单元格正确设置了自定义类单元格,如图所示:
第一次渲染单元格时,一切都按预期工作,如图所示:
但是之后,任何加载或重新加载的单元格都会从单元格分隔符和单元格标签中获得左边的插图,这会受到图像大小的影响(就像单元格在内部忽略了我对imageView的新框架一样),如图所示: / p>
如果我在我的自定义单元格的layoutSubviews()
中强制插入内容,一切看起来都很好。
override func layoutSubviews() {
super.layoutSubviews()
self.imageView?.contentMode = UIViewContentMode.Center
self.imageView?.frame = CGRectMake(10, 10, 40, 40)
self.imageView?.layer.cornerRadius = (self.imageView?.frame.width)! / 2
self.imageView?.layer.masksToBounds = true
self.imageView?.layer.backgroundColor = UIColor.init(colorLiteralRed: 0.8,
green: 0.8,
blue: 0.8,
alpha: 1.0).CGColor
let _correctedLeftInset = (self.imageView?.frame.width)!
+ ((self.imageView?.frame.origin.x)! * 2)
self.textLabel?.frame.origin.x = _correctedLeftInset
self.separatorInset.left = _correctedLeftInset
}
但是我觉得这个被迫留下来是一个不必要的黑客,因为有些东西丢失或错误。你知道为什么吗?
如果您想了解有关此问题的更多信息,请与我们联系。
提前致谢。