UITableViewCell 及其出口是否在“dequeueReusableCellWithIdentifier”之后初始化?
Is a UITableViewCell and its outlets initialized after `dequeueReusableCellWithIdentifier`?
是否在使用 dequeueReusableCellWithIdentifier
实例化后立即访问自定义 UITableViewCell
的插座?
例如
class MyCell: UITableViewCell {
@IBOutlet weak var myImageView: UIImageView!
var image: UIImage?
override func awakeFromNib() {
update()
}
func update() {
myImageView.image = image
}
}
class MyViewController: UIView() {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCellIdentifier") as! MyCell
cell.image = UIImage(...)
cell.update()
}
}
我经常使用此实现,但很少使用 (<0.001%) 我收到指向第 myImageView.image = image
行的崩溃报告。
更新:
到目前为止,仅针对一种特定实现观察到崩溃,其中 1 个插座链接到自定义单元格中的多个 UIImageView()
,因为它们共享相同的 class.
简单的方法dequeueReusableCellWithIdentifier:
returns一个不安全的可选方法。
改用此方法是安全的,因为它 returns 是一个非可选单元格
let cell = tableView.dequeueReusableCellWithIdentifier("MyCellIdentifier",
forIndexPath: indexPath) as! MyCell
由于 UIImageView
对象的 image
属性 可以是 nil
建议将相关 UIImage
属性声明为可选(?
) 而不是没有默认初始值设定项 (()
)
的隐式解包可选 (!
)
是否在使用 dequeueReusableCellWithIdentifier
实例化后立即访问自定义 UITableViewCell
的插座?
例如
class MyCell: UITableViewCell {
@IBOutlet weak var myImageView: UIImageView!
var image: UIImage?
override func awakeFromNib() {
update()
}
func update() {
myImageView.image = image
}
}
class MyViewController: UIView() {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCellIdentifier") as! MyCell
cell.image = UIImage(...)
cell.update()
}
}
我经常使用此实现,但很少使用 (<0.001%) 我收到指向第 myImageView.image = image
行的崩溃报告。
更新:
到目前为止,仅针对一种特定实现观察到崩溃,其中 1 个插座链接到自定义单元格中的多个 UIImageView()
,因为它们共享相同的 class.
简单的方法dequeueReusableCellWithIdentifier:
returns一个不安全的可选方法。
改用此方法是安全的,因为它 returns 是一个非可选单元格
let cell = tableView.dequeueReusableCellWithIdentifier("MyCellIdentifier",
forIndexPath: indexPath) as! MyCell
由于 UIImageView
对象的 image
属性 可以是 nil
建议将相关 UIImage
属性声明为可选(?
) 而不是没有默认初始值设定项 (()
)
!
)