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
}
ervanux
  • 281
  • 3
  • 9
  • 2
    I suppose the reason is table view cell is reused, so they are not actually deleted, just return to the cell pool actually. – reviver Oct 19 '15 at 09:17
  • 4
    Your cell wont be deinitialized until table view is deinitialized and thats because tablew view hold strong reference of it – Zell B. Oct 19 '15 at 09:25

1 Answers1

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

RodolfoAntonici
  • 1,555
  • 21
  • 34
  • What if I want to remove an observer from my cell? Where should one do that? – Tycho Pandelaar Nov 29 '18 at 12:39
  • 3
    @P5ycH0 as of iOS 9 it is no longer required to remove the observer in deinit. See the Discussion section here: https://developer.apple.com/documentation/foundation/notificationcenter/1407263-removeobserver. But as mentioned above you probably want to stop listening in prepareForReuse and start again in cellForRowAt. – Murray Sagal Nov 30 '18 at 18:21