Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
}
like image 621
ervanux Avatar asked Oct 19 '15 09:10

ervanux


People also ask

Why deinit is not called?

If you return nil from a failable initializer, init?() , or if you throw an error from a throwing initializer, init() throws , then deinit will not get called. This means you must clean up any manually allocated memory or other resources before you return nil or throw .

Which is the content of UITableViewCell?

Overview. A UITableViewCell object is a specialized type of view that manages the content of a single table row.


1 Answers

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

like image 164
RodolfoAntonici Avatar answered Sep 25 '22 08:09

RodolfoAntonici