Jap*_*apa 2 core-data fetch uitableview nsfetchedresultscontroller nsfetchrequest
我有一个包含多个实体的核心数据模型,每个实体对应一个视图控制器,用户为每个实体插入信息,例如:
轮胎(第一个实体) - 类型(第一个属性),成本(第二个属性).玻璃(第二个实体) - 颜色(第一个属性),大小(第二个属性),成本(第三个属性).
对于每个实体,我有一个带有文本字段的MVC,用户为每个实体插入信息,因此如果按下轮胎按钮,则为其他实体插入一种轮胎,成本..等等.
当我拉一个实体时,一切顺利,我在文本字段中插入信息然后按保存,它很好地出现在我的tableview上!但是,正如您已经知道的那样,我希望其余的实体插入到我的tableview和不同的部分中.
我试过"搞乱""cellForRowAtIndexPath",也在我的"fetchedResultsController"方法中使用"if"和"swich",但没有成功.我在这里缺少什么?!?!?!
谢谢,对不起我的英语.
您需要一个实体才能NSFetchedResultsController正常工作并利用其高级内存和性能功能.
因此,修改您的数据模型并创建一个新实体,Item即其他实体的抽象父实体.(您可以在模型编辑器中指定父实体.)确保所有项(例如名称)共享的属性属于父项.这些应该是您要在表视图中显示的属性.
现在,您可以为每个子实体添加唯一的NSString或NSNumber属性type,并可以通过此键轻松排序NSFetchedResultsController.
来自Apple的[核心数据编程指南] [1]:
If you define an entity inheritance hierarchy (see "Entity Inheritance"), when you specify a super-entity as the entity for a fetch request, the request returns all matching instances of the super-entity and of sub-entities. In some applications, you might specify a super-entity as being abstract (see "Abstract Entities"). To fetch matching instances of all concrete sub-entities of the abstract entity, you set the entity for fetch specification to be the abstract entity. In the case of the domain described in "Abstract Entities," if you specify a fetch request with the Graphic entity, the fetch returns matching instances of Circle, TextArea, and Line.
So you fetch all Items, sort by type, and for each cell you determine the type and fill your text labels with the appropriate properties.