简体   繁体   中英

Is a UITableViewCell and its outlets initialized after `dequeueReusableCellWithIdentifier`?

Is it save to access an outlet of a custom UITableViewCell right after instantiating it with dequeueReusableCellWithIdentifier ?

Eg

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()
    }
}

I have used this implementation a lot but very rarely (<0.001%) I get a crash report pointing to line myImageView.image = image .

UPDATE: So far the crashes have been observed only for one specific implementation where 1 outlet is linked to many UIImageView() in custom cells because they share the same class.

The simple method dequeueReusableCellWithIdentifier: returns an optional which is not safe.

Use this method instead which is safe because it returns an non-optional cell

let cell = tableView.dequeueReusableCellWithIdentifier("MyCellIdentifier",
           forIndexPath: indexPath) as! MyCell

Since the image property of an UIImageView object can be nil it's recommended to declare related UIImage properties as optional ( ? ) rather than implicit unwrapped optional ( ! ) without the default initializer ( () )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM