UITableViewCell object deinit never called
Asked Answered
G

1

12

the problem is in my project. But I also tried in new xcode draft project that Master-Detail Application one.

I have created base Cell class and mapped the cell on storyboard. After delete operation deinit never called. There is no strong or weak any reference to cell.

class Cell:UITableViewCell {
    deinit{
        print("Deinit Called")
    }
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! Cell

    let object = objects[indexPath.row] as! NSDate
    cell.textLabel!.text = object.description
    return cell
}
Glori answered 19/10, 2015 at 9:8 Comment(2)
I suppose the reason is table view cell is reused, so they are not actually deleted, just return to the cell pool actually.Unroll
Your cell wont be deinitialized until table view is deinitialized and thats because tablew view hold strong reference of itHillaryhillbilly
R
10

UITableViewCells work as a lazy var, they are initialized on the first use and are kept alive as long the UITableView that initialized them is alive, that happens on the moment the register:forIdentifier: is called. If you're using the Storyboard that's is done for you.

Why it's done? Every cell can be reutilized using dequeueReusableCellWithIdentifier, so the UITableView keep a instance alive to be utilized if needed.

If you need to clear up some data, just call prepareForReuse on the UITableViewCell

Rearward answered 21/4, 2017 at 16:57 Comment(2)
What if I want to remove an observer from my cell? Where should one do that?Lahr
@P5ycH0 as of iOS 9 it is no longer required to remove the observer in deinit. See the Discussion section here: developer.apple.com/documentation/foundation/notificationcenter/…. But as mentioned above you probably want to stop listening in prepareForReuse and start again in cellForRowAt.Sepulchral

© 2022 - 2024 — McMap. All rights reserved.