10

问题出在我的项目中。但我也在新的 xcode 草案项目中尝试了 Master-Detail Application 之一。

我创建了基本单元类并将单元映射到情节提要上。删除操作后 deinit 从未调用过。对单元格没有强或弱的任何引用。

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
}
4

1 回答 1

7

UITableViewCells 作为一个惰性 var 工作,它们在第一次使用时被初始化,并且只要初始化它们的 UITableView 处于活动状态,它们就会保持活动状态,这发生在 register:forIdentifier: 被调用的那一刻。如果您使用的是为您完成的故事板。

为什么完成了?每个单元格都可以使用 dequeueReusableCellWithIdentifier 重新利用,因此 UITableView 保持一个实例处于活动状态,以便在需要时使用。

如果需要清理一些数据,只需在 UITableViewCell 上调用 prepareForReuse

于 2017-04-21T16:57:15.413 回答