对核心数据表进行分组查看手动创建的部分中的行

发布于 2024-10-13 08:26:38 字数 816 浏览 5 评论 0原文

我有一个包含大约 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺深海 2024-10-20 08:26:38

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

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

我有一个核心数据实体 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;
}

我的想法是,我可以在引用核心数据时使用 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;

(root)

->CompanyTpl (array)

--> Item 0 (Dictionary)

---> Section (String) = "General"

---> Children (Array
)
------> Item 0 (Dictionary)

----------> Key = "name"

----------> Value = "Company Name" ...

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.

- (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;
}

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.

梦言归人 2024-10-20 08:26:38

我已经接近答案了,但我仍然无法连接核心数据项,以便它们位于某些部分。

#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 个部分,名称目前并不重要,然后我为每个部分创建不同数量的行。

到目前为止,一切都很好。

我遇到的问题是让核心数据理解在section0.row0中我希望textLabel说出公司名称等。

我认为所有这些都必须在字典中,布置出整个结构我想要,以及要显示的标签;然后在 cellForRowAtIndexPath 中显示我想要的字典中的数组。

IE:

[根] CompanyTpl(数组)

-->项目 0(字典)

----->类别(字符串)=“常规”

----->数据(数组)

--------->第 0 项(字典)

----------------> cdFieldName:名称

---------------->显示:“公司名称”

其中 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.

#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;
}

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:

[root] CompanyTpl (array)

--> Item 0 (dictionary)

-----> Category (string) = "General"

-----> Data (Array)

---------> Item 0 (Dictionary)

---------------> cdFieldName: name

---------------> display: "Company name"

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文