在我们当前的应用程序中,我们需要在自定义 UITableViewCells 的 Dispose() 中执行一些清理操作。
不幸的是,即使我们调用 TableView.Dispose(),我们的单元格也不会立即被处理。相反,稍后,终结器将调用 Dispose(false)。 由于我们是由终结器调用的,所以我们不应该在这里执行清理:对象可能处于不可预测的状态,并且无法确定它发生的时间。
来自微软文档:
The finalizer is called when the GC detects that an object is eligible for collection. This happens at some undetermined period of time after the resource is not needed anymore.
还有:
If the method is invoked from the finalizer (disposing is false), other objects should not be accessed. The reason is that objects are finalized in an unpredictable order and so they, or any of their dependencies, might already have been finalized.
是否有解决方案可以在释放 tableview 时调用自定义单元格的 Dispose() 方法?
最佳答案
如果在表本身上调用 Dispose 时确实需要对每个单元格调用 Dispose,则需要自己处理:
- 子类 UITableView。
- 将您使用的所有单元格存储在此类的单元格列表中。
- 重写 View 上的 Dispose,并对列表中的每个单元格调用 Dispose。
关于c# - MonoTouch 确定性处理单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24019899/