在手动创建的部分中对核心数据tableView进行分组

zar*_*don 5 core-data manual uitableview

我有一个Company包含大约20个字段的实体,我想使用带有手动创建的节标题的分组tableView(即:General,Financial,Misc.),但我不确定如何让Core Data了解如何处理这些节标题并使它们仅与我想在这些组中显示的数据相关.

例如,名称,徽标等将在一般情况下,预算,现金将属于财务等.

基本上,我想控制Core数据中的哪些数据被放入每个类别并显示它.

在核心书籍示例中有以下代码:

/*
 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];
}
Run Code Online (Sandbox Code Playgroud)

但是,如何让它理解这些部分不在Core数据中,而是手动创建?

zar*_*don 1

我现在已经找到了问题的答案,我不知道它是否是正确的方法,但它正在起作用,欢迎发表评论。

只是为了澄清问题是什么以及我正在尝试做什么。

我有一个核心数据实体,company其中包含大约 10 个左右的字段,但是我不想一次性列出所有字段,而是想对输出的字段进行分组。

例如,我有大约 6 个与现金相关的字段,例如“cash”、“marketingBudget”、“seoBudget”等,我想将这些数据分组到 tableView 上,但问题是我不知道如何设置使 table.field.x 属于 group.x 的关系,依此类推。

我得到的答案是使用 PLIST/字典,它几乎反映了核心数据实体的结构;并将结构分配给我想要显示的组。

我的字典看起来像这样;

(根)

->CompanyTpl(数组)

--> 项目 0(字典)

---> 部分(字符串)=“常规”

---> 子项(数组) ------> 项目 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;
}
Run Code Online (Sandbox Code Playgroud)

我的想法是,我可以在引用核心数据时使用 KEY 来获取其内容并将其显示在 tableView 控制器上的 cellForRowAtIndexPath cell.textLabel.text 值上。

人们可以更深入一点,在 PLIST 中获得更多信息,例如副标题应该是什么,等等。

无论如何,欢迎评论和想法。

谢谢。