2011-01-23 42 views
5

我有一個約有20個字段的Company實體,我想用手動創建節標題(即:General,Financial,Misc。)來使用分組tableView。但是我不確定如何讓Core Data瞭解如何處理這些節標題,並使它們僅與我想在這些組中顯示的數據相關。在手動創建節中對核心數據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]; 
} 

但是我怎麼讓它明白,部分是不是在覈心數據,但手動創建?

回答

1

我現在已經得到了我的問題的答案,我不知道它的正確方法,但它工作並歡迎評論。

只是爲了澄清問題是什麼,以及我正在嘗試做什麼。

我有一個核心數據實體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中的詳細信息,如字幕應該是什麼,等等。

無論如何,歡迎評論和想法。

謝謝。

+0

我發現http://code.google.com/p/coredatalibrary/這可能對那些想要更高級控制這種事情的人有用。 – zardon 2011-02-21 11:57:15

0

我已經接近一個答案了,但是我仍然無法連接核心數據項,以至於它們在某些部分。

#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中顯示我想要的字典中的數組。

即:

[根] CompanyTpl(數組)

- >項目0(字典)

----->類別(字符串)= 「常規」

----->數據(數組)

--------->項目0(詞典)

---------------> cdFieldName:命名

--------------->顯示: 「公司名稱」

其中cdFi​​eldName是我想要顯示的Core Data內的字段名稱的引用。

如果有另一種方法可以做到這一點,我會有興趣找出答案。

謝謝。