对核心数据表进行分组查看手动创建的部分中的行
我有一个包含大约 20 个字段的 Company
实体,我想使用带有手动创建的节标题(即:常规、财务、杂项)的分组 tableView,但我不确定如何制作核心数据了解如何处理这些节标题并使它们仅与我想要在这些组中显示的数据相关。
例如,名称、徽标等将归入常规,预算、现金将归入财务等。
基本上,我想控制核心数据中的哪些数据放入每个类别并显示它。
在核心书籍示例中有这样的代码:
/*
The data source methods are handled primarily by the fetch results controller
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[fetchedResultsController sections] count];
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
但是我如何让它理解这些部分不在核心数据中,而是手动创建的?
I have a Company
entity with about 20 fields and I'm wanting to use a grouped tableView with manually created section headers (ie: General, Financial, Misc.), but I am unsure of how to make Core Data understand how to treat these section headers and make them relate only to the data I want to show in these groups.
For example, name, logo, etc would go under General, budgets, cash would go under financial, etc.
Basically, I want to control what data from Core data gets put into each category and display it.
In the Core books sample there is this code:
/*
The data source methods are handled primarily by the fetch results controller
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[fetchedResultsController sections] count];
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
But how do I make it understand that the sections aren't in Core data, but created manually?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我现在已经找到了问题的答案,我不知道它是否是正确的方法,但它正在起作用,欢迎发表评论。
只是为了澄清问题是什么以及我正在尝试做什么。
我有一个核心数据实体
company
,其中包含大约 10 个左右的字段,但是我不想一次性列出所有字段,而是想对输出的字段进行分组。例如,我有大约 6 个与现金相关的字段,例如“cash”、“marketingBudget”、“seoBudget”等,我想将这些数据分组到 tableView 上,但问题是我不知道如何设置使 table.field.x 属于 group.x 的关系,依此类推。
我得到的答案是使用 PLIST/字典,它几乎反映了核心数据实体的结构;并将结构分配给我想要显示的组。
我的字典看起来像这样;
其中
Key
将是核心数据使用和显示其内容(如果需要)的引用。Value
将显示在 cellForRowAtIndexPath 处。因此,在我的代码中,我基本上浏览了该部分(我指的是 tableView 部分),然后从 PLIST 中找到相关的子信息;并获取键/值并在需要时使用它。
这是代码的精简版本。
我的想法是,我可以在引用核心数据时使用 KEY 来获取其内容并将其显示在 tableView 控制器上的 cellForRowAtIndexPath cell.textLabel.text 值上。
人们可以更深入一点,并在 PLIST 中获得更多信息,例如副标题应该是什么,等等。
无论如何,欢迎发表评论和想法。
谢谢。
I've got an answer to my problem now, I do not know if its the correct approach, but it is working and would welcome comments.
Just to clarify what the problem was, and what I was trying to do.
I have a core data entity
company
with about 10 or so fields inside them, however rather than listing them all in one go, I wanted to group the outputted fields.For example, I have about 6 fields relating to cash such as "cash", "marketingBudget", "seoBudget", etc and I wanted to group this data on the tableView, but the problem was I didn't know how to set up a relationship so that table.field.x belonged to group.x, and so on.
The answer I came to was to use a PLIST/dictionary that pretty much mirrors the structure of the core data entity; and assign the structure to the groups I want to display.
My dictionary looks like this;
Where the
Key
would be a reference for Core Data to use and display its contents, if required.Where the
Value
would be would to display at the cellForRowAtIndexPath.So, in my code I basically went through the section (by which I mean tableView section) and then find the correlating children info from the PLIST; and get the Key/Value and use this as and when required.
Here is a cut down version of the code.
The idea would be that I can use the KEY when referencing Core Data to grab its contents and display it on the tableView controller at cellForRowAtIndexPath cell.textLabel.text value.
One could go a little further in depth and have more info in the PLIST such as what the subtitle should be, etc.
Anyway, would welcome comments and thoughts.
Thanks.
我已经接近答案了,但我仍然无法连接核心数据项,以便它们位于某些部分。
它的作用是手动创建 4 个部分,名称目前并不重要,然后我为每个部分创建不同数量的行。
到目前为止,一切都很好。
我遇到的问题是让核心数据理解在section0.row0中我希望textLabel说出公司名称等。
我认为所有这些都必须在字典中,布置出整个结构我想要,以及要显示的标签;然后在 cellForRowAtIndexPath 中显示我想要的字典中的数组。
IE:
其中 cdFieldName 是对我要显示的 Core Data 中的字段名称的引用。
如果有其他方法可以做到这一点,我有兴趣找出答案。
谢谢。
I have got a little closer to an answer, but I am still having trouble hooking up core data items so that they are in certain sections.
What this does is manually create 4 sections, the names don't matter at the moment, and then I create a different number of rows for each section.
So far, so good.
The problem I'm having is making Core Data understand that in section0.row0 I want the textLabel to say the the company name etc.
I'm thinking that all of this has to be in a dictionary, laying-out the entire structure that I want, and the labels to display; and then in cellForRowAtIndexPath I display the array within the dictionary that I want.
ie:
Where cdFieldName is a reference to the field name within Core Data I want to display.
If there is an alternative way to do this, I'd be interested in finding out.
Thanks.