我現在已經得到了我的問題的答案,我不知道它的正確方法,但它工作並歡迎評論。
只是爲了澄清問題是什麼,以及我正在嘗試做什麼。
我有一個核心數據實體company
,裏面有大約10個左右的字段,但是我不想將它們全部一一列出,我想對輸出字段進行分組。
例如,我有大約6個與現金有關的領域,比如「cash」,「marketingBudget」,「seoBudget」等,我想將這些數據分組在tableView上,但問題是我不知道如何建立關係以便table.field.x屬於group.x,依此類推。
我來的答案是使用PLIST /字典,它幾乎反映了核心數據實體的結構;並將結構分配給我想要顯示的組。
我的字典看起來像這樣;
(根)
- > CompanyTpl(數組)
- >項目0(詞典)
--->科(字符串)= 「常規」
---> Children(Array ) ------> Item 0(Dictionary)
---------- >重點=凡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值的tableView控制器上顯示它時,我可以使用KEY。
人們可以去遠一點的深度和在plist中的詳細信息,如字幕應該是什麼,等等。
無論如何,歡迎評論和想法。
謝謝。
我發現http://code.google.com/p/coredatalibrary/這可能對那些想要更高級控制這種事情的人有用。 – zardon 2011-02-21 11:57:15