ervanux
ervanux

Reputation: 281

UITableViewCell object deinit never called

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
}

Upvotes: 12

Views: 8642

Answers (1)

RodolfoAntonici
RodolfoAntonici

Reputation: 1625

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

Upvotes: 10

Related Questions