Ho una risposta al mio problema ora, non so se è l'approccio corretto, ma funziona e gradirei commenti.
Giusto per chiarire qual era il problema e cosa stavo cercando di fare.
Ho un'entità dati nucleo company
con circa 10 campi o meno all'interno di essi, tuttavia piuttosto che elencarli tutti in una volta sola, volevo raggruppare i campi emessi.
Ad esempio, ho circa 6 campi relativi a contante come "cash", "marketingBudget", "seoBudget", ecc. E volevo raggruppare questi dati sul TableView, ma il problema era che non sapevo come impostare una relazione in modo che table.field.x appartenga a group.x e così via.
La risposta a cui sono giunto è stata l'utilizzo di un dizionario PLIST che rispecchia pressoché la struttura dell'entità dei dati di base; e assegna la struttura ai gruppi che voglio mostrare.
Il mio dizionario ha questo aspetto;
(root)
-> CompanyTpl (array)
-> Voce 0 (dizionario)
---> Sezione (String) = "Generale"
---> bambini (Array ) ------> Articolo 0 (dizionario)
---------- > Chiave = "nome"
----------> Value = "Nome Azienda" ...
Qualora il Key
sarebbe un punto di riferimento per i Core Data da usare e visualizzazione il suo contenuto, se richiesto.
Dove lo Value
dovrebbe essere visualizzato in cellForRowAtIndexPath.
Così, nel mio codice ho praticamente esaminato la sezione (con cui intendo la sezione tableView) e poi trovare le informazioni correlate ai bambini da PLIST; e ottieni la chiave/valore e usalo come e quando richiesto.
Ecco una versione ridotta del codice.
- (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;
}
L'idea sarebbe quella di poter utilizzare la chiave quando si fa riferimento Core Data per afferrare il suo contenuto e visualizzarlo sul controller tableView al valore cellForRowAtIndexPath cell.textLabel.text.
Si potrebbe andare un po 'più a fondo e avere più informazioni nel PLIST come quello che il sottotitolo dovrebbe essere, ecc
In ogni caso, sarebbe il benvenuto commenti e pensieri.
Grazie.
Ho trovato http://code.google.com/p/coredatalibrary/ che potrebbe essere utile per le persone che desiderano un controllo più avanzato su questo tipo di cose. – zardon