Grouping tableView row master data in manually created partitions

I have an object Companywith about 20 fields, and I want to use a grouped tableView with manually created section headers (for example, General, Financial, Misc.), But I'm not sure how to make Core Data how to process these section headers and only make them related with the data that I want to show in these groups.

For example, name, logo, etc. will be under the heading "General", "budgets", "cash" will be transferred to financial, etc.

Basically, I want to control which data from Core data falls into each category and display it.

The core books sample has 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 understand that partitions are not part of the master data, but created manually?

+5
2

, , , .

, , .

company 10 , , , .

, 6 , , "cash", "marketingBudget", "seoBudget" .., tableView, , , , table.field.x group.x ..

, , PLIST/dictionary, ; , .

:

()

- > CompanyTpl ()

- > 0 ()

--- > (String) = ""

--- > ( ) ------ > 0 ()

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

---------- > = " "...

Key Core Data, , .

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 Core Data, tableView cellForRowAtIndexPath cell.textLabel.text.

PLIST, , ..

, .

.

+1

, , .

#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 , , .

.

, , , Core Data , section0.row0 , textLabel ..

, , , , ; cellForRowAtIndexPath , .

[root] CompanyTpl ()

- > 0 ()

----- > () = ""

----- > ()

--------- > 0 ()

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

--------------- > : " "

cdFieldName Core Data, .

, .

.

0

All Articles