KVO观察NSMangedObject在UITableViewCell [英] KVO observing of NSMangedObject in a UITableViewCell
问题描述
我使用自定义 UITableViewCell
显示我的模型中 NSMangedObject
的属性。
每个单元格都保存对被管对象的引用。
我也通过调用 addObserver:...来监听对象属性的变化。
问题是可以从模型中删除对象,并且当发生这种情况时我想取消注册。
p>有一个类似的问题这里,但建议的解决方案是在对象上使用 prepareForDeletion
,然后手动通知单元格。这不是一个非常漂亮的解决方案。
我想知道这些情况下推荐的解决方案是什么,更多的是 - 观察KVO对托管对象的改变确实
不,你不应该使用KVO来实现。您需要使用 NSFetchedResultsController
,以便您的tableview单元格将使用 NSFetchedResultsController
委托方法自动更新。 p>
请参阅相关的文档。
I'm using a custom UITableViewCell
to display properties of a NSMangedObject
from my model.
Each cell holds a reference to the managed object.
I also listen to changes in properties of the object by calling addObserver:…
on the object.
The problem is that the objects can be removed from the model, and I want to unregister when this happens.
There is a similar question here, but the suggested solution there is to use prepareForDeletion
on the object, and then manually notify the cells. This is not a very pretty solution.
I was wondering what is the recommended solution for these cases, and more then that - is observing KVO changes on the managed object is indeed the best approach to what I am trying to achieve.
No, you should not use KVO for this. You need to use an NSFetchedResultsController
, so that your tableview cells will be automatically updated using the NSFetchedResultsController
delegate methods.
Please see the relevant documentation.
这篇关于KVO观察NSMangedObject在UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!