KVO观察NSMangedObject在UITableViewCell [英] KVO observing of NSMangedObject in a UITableViewCell

查看:162
本文介绍了KVO观察NSMangedObject在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆