将核心数据表视图行分组为手动创建的部分
-
23-10-2019 - |
题
我有一个 Company
具有大约20个字段的实体,我想使用手动创建的截面标题(即:一般,财务,杂项)的分组表视图,但我不确定如何使核心数据了解如何处理这些部分的标题并制作标题它们仅与我要在这些组中显示的数据有关。
例如,姓名,徽标等将在一般情况下进行,预算,现金将在财务状态下,等等。
基本上,我想控制将来自核心数据的数据放入每个类别中并显示它。
在核心书籍中,有此代码:
/*
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];
}
但是,我如何理解这些部分不在核心数据中,而是手动创建的?
解决方案
我现在有一个答案,我不知道它是正确的方法,但是它正在奏效,欢迎您的评论。
只是为了澄清问题是什么,以及我想做什么。
我有一个核心数据实体 company
其中有大约10个左右的字段,但是我想对输出的字段进行分组。
例如,我有大约6个与现金有关的字段,例如“现金”,“ MarketingBudget”,“ seobudget”等,我想将这些数据分组在表观视图上,但是问题是我不知道如何设置一种关系,使得table.field.x属于group.x,依此类推。
我提到的答案是使用PLIST/词典几乎反映了核心数据实体的结构;并将结构分配给我要显示的组。
我的词典看起来像这样。
(根)
- > CompanyTPL(数组)
- >项目0(字典)
---> pection(string)=“常规”
--->儿童(数组)------>项目0(字典)
----------->键=“名称”
----------->值=“公司名称” ...
在哪里 Key
如果需要,将是用于使用和显示其内容的核心数据的参考。
在哪里 Value
将在CellForrowatIndExpath上显示。
因此,在我的代码中,我基本上浏览了该部分(我的意思是TableView部分),然后从PLIST中找到相关的儿童信息;并获取键/值,并在需要时使用它。
这是代码的剪切版本。
- (void)viewDidLoad {
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"CompanyTpl" ofType:@"plist"];
self.companyDictionary = [[NSDictionary dictionaryWithContentsOfFile:plistPath] retain];
// self.tableDataSource is a NSMutableArray
self.tableDataSource = [self.companyDictionary objectForKey:@"CompanyTpl"];
// Debugging info
NSLog(@"Section = 0");
NSLog(@"%@", [self.tableDataSource objectAtIndex:0]);
NSLog(@"Section Name = %@", [[self.tableDataSource objectAtIndex:0] objectForKey:@"Section"]);
NSArray *sectionChildren = [[self.tableDataSource objectAtIndex:0] objectForKey:@"Data"];
NSLog(@"Section Children = %@", sectionChildren);
NSLog(@"Count of Section Children = %d", [sectionChildren count]);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return ([self.tableDataSource count]);
}
// Section header
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *title = nil;
title = [[self.tableDataSource objectAtIndex:section] objectForKey:@"Section"];
return title;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger rows = 0;
NSArray *sectionChildren = [[self.tableDataSource objectAtIndex:section] objectForKey:@"Data"];
NSLog(@"Section Children = %@", sectionChildren);
NSLog(@"Count of Section Children = %d", [sectionChildren count]);
rows = [sectionChildren count];
return rows;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *sectionChildren = [[self.tableDataSource objectAtIndex:indexPath.section] objectForKey:@"Data"];
NSDictionary *sectionChildrenData = [sectionChildren objectAtIndex:indexPath.row];
//NSLog(@"Section Children data = %@", sectionChildrenData);
NSString *scKey = [sectionChildrenData objectForKey:@"Key"];
NSString *scValue = [sectionChildrenData objectForKey:@"Value"];
NSLog(@"scKey = %@", scKey);
// Grab the data from Core Data using the scKey
static NSString *CellIdentifier = @"defaultCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
//cell.textLabel.text = @"test";
cell.textLabel.text = scValue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
这个想法是,在引用核心数据以获取其内容时,我可以使用密钥,并在CellForrowatIndExpath Cell.TextLabel.text值上显示在桌面控制器上。
一个人可以深入了解一些信息,并在PLIST中提供更多信息,例如字幕应该是什么,等等。
无论如何,欢迎评论和想法。
谢谢。
其他提示
我有点接近答案,但是我仍然难以连接核心数据项,以便它们在某些部分。
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//return [[fetchedResultsController sections] count];
//NSLog(@"%d", [self.sectionArray count] );
return 4;
}
// Section header
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *title = nil;
switch (section) {
case 0:
title = @"General";
break;
case 1:
title = @"Finanical";
break;
case 2:
title = @"Category A";
break;
case 3:
title = @"Category B";
break;
case 4:
title = @"Misc";
break;
default:
break;
}
return title;
}
// 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];
*/
NSInteger rows = 0;
// The number of rows depend on the section
switch (section) {
case 0:
rows = 2;
break;
case 1:
rows = 3;
break;
case 2:
rows = 4;
break;
default:
break;
}
return rows;
}
这样做的是手动创建4个部分,目前名称不重要,然后我为每个部分创建不同数量的行。
到目前为止,一切都很好。
我遇到的问题是使核心数据理解,在0.row0中,我希望TextLabel说公司名称等。
我认为所有这些都必须在词典中,铺设了我想要的整个结构以及要显示的标签。然后在CellForrowatIndExpath中,我在我想要的字典中显示数组。
IE:
root] CompanyTPL(数组)
- >项目0(字典)
----->类别(string)=“常规”
----->数据(数组)
--------->项目0(字典)
------------------> CDFIELDNAME:名称
----------------->显示:“公司名称”
cdfieldname是我要显示的核心数据中字段名称的引用。
如果有另一种方法可以做到这一点,我将有兴趣找出。
谢谢。