【发布时间】:2017-12-16 02:33:20
【问题描述】:
我有一个自定义 UITableViewCell,我想在我的表格视图中使用它。这是我的单元格代码:
class ReflectionCell: UITableViewCell {
@IBOutlet weak var header: UILabel!
@IBOutlet weak var content: UILabel!
@IBOutlet weak var author: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
init(data: Reflection) {
self.header.text = data.title
self.content.text = data.content
self.author.text = data.author.name
super.init(style: UITableViewCellStyle.default, reuseIdentifier: "reflectionCell")
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
我有一个模型类Reflection,我想用它来初始化单元格。但是,在我的视图控制器中,我需要使用tableView.dequeueReusableCell(withIdentifier: "reflectionCell", for: indexPath)。有什么方法可以让我使用我制作的自定义初始化程序吗?
【问题讨论】:
-
TableViewCell 被重用,你不要那样做。您可以做的是创建一个方法
updateWith(data:Reflection)并设置标签值,或者创建一个属性var data:Reflection并在didSet上设置标签的值。
标签: ios swift uitableview