2011-05-06 78 views
0

我正在創建一個Ipad應用程序,它將充當酒單。計數numberOfRowsInSection根據部分

我的酒單是按部分組織的,每一部分都有不同數量的行。

我的問題是,我沒有設法根據部分計算行數。

我解析此JSON文件:

{"posts":[{"appellation":"Bordeaux Sup\u00e9rieur","nom":"Chateau David Beaulieu","annee":"2008","prix":"25"},{"appellation":"Blaye","nom":"Chateau L'Embrun","annee":"2006","prix":"35"},{"appellation":"Moulis","nom":"Ch\u00e2teau Poujeaux","annee":"1990","prix":"75"},{"appellation":"Moulis","nom":"Ch\u00e2teau Chasse-Spleen","annee":"1990","prix":"80"}]} 

通常我應該有2排的部分「慕里斯」和一排的人。 我想其中的幾個解決方案:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

NSString *appellation=[[self.rows objectAtIndex:section] objectForKey:@"appellation"]; 
NSLog(@"appellation: %@",appellation); 

NSArray *cpt=[dict objectForKey:appellation]; 
NSLog(@"cpt: %@",cpt); 


return [cpt count]; 
} 

返回

2011-05-03 13:49:12.947 cha[9913:207] appellation: Moulis 
2011-05-03 13:49:12.947 cha[9913:207] cpt: (null) 
2011-05-03 13:49:12.948 cha[9913:207] appellation: Bordeaux Supérieur 
2011-05-03 13:49:12.949 cha[9913:207] cpt: (null) 
2011-05-03 13:49:12.951 cha[9913:207] appellation: Blaye 
2011-05-03 13:49:12.952 cha[9913:207] cpt: (null) 

它返回好的部分,但不是好線。

的NSLog(@ 「字典%@」,字典)返回

2011-05-06 10:13:56.835 chan[2751:207] dict { 
    posts =  (
       { 
      annee = 2008; 
      appellation = "Bordeaux Sup\U00e9rieur"; 
      nom = "Chateau David Beaulieu"; 
      prix = 25; 
     }, 
       { 
      annee = 2006; 
      appellation = Blaye; 
      nom = "Chateau L'Embrun"; 
      prix = 35; 
     }, 
       { 
      annee = 1990; 
      appellation = Moulis; 
      nom = "Chateau Poujeaux"; 
      prix = 75; 
     }, 
       { 
      annee = 1990; 
      appellation = Moulis; 
      nom = "Chateau Chasse Spleen"; 
      prix = 80; 

    ); 

} 

此代碼的工作完美,但現在我想我的按字母順序排列的酒單。我的字典是按字母順序創建的。我這allKeys排序結果隨機文檔中看到的,所以我用:

[[[appDelegate.mutableDic allKeys]sortedArrayUsingSelector:@selector(compare:)] objectAtIndex:section]; 
在viewForHeaderInSection

,並

NSDictionary *ligneVin; 
ligneVin=[[[appDelegate.mutableDic allValues ] objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 
中的cellForRowAtIndexPath

,但葡萄酒不符合任何更多與部分。

任何形式的幫助都非常感謝。

謝謝;)

+0

檢查內容在 – visakh7 2011-05-06 08:27:21

回答

1

什麼你到底想?我覺得應該是

return [[self.rows objectAtIndex:section]count];

那麼你得到的對象「的帖子」的數量。你嘗試的是獲得一個名爲「Moulis」的對象(例如),但是你沒有那個對象。您的對象包含屬性名稱爲「稱謂」,其值爲「Moulis」。你的對象還包含關鍵字nom,annee和prix。所有在一起是一個對象。

//編輯:實際上這不會工作,因爲self.rows已經是「帖子」對象。所以你必須返回self.rows.count

那麼你可以通過訪問每個對象cellForRowAtIndexPath:

NSDictionary *dic = [self.rows objectAtIndex:indexPath.row]; 
[dic objectForKey:@"appellation"]; 
[dic objectForKey:@"nom"]; 
[dic objectForKey:@"annee"]; 
[dic objectForKey:@"prix"]; 

// EDIT2:
確定你添加的信息,現在足以創建你一個工作代碼;) 在你的位置我會首先重組你的數據:

NSMutableDictionary *mutableDic = [NSMutableDictionary dictionary]; 
for (NSDictionary *dic in [wineList objectForKey:@"posts"]) { 
    NSString *key = [dic valueForKey:@"appellation"]; 
    if ([mutableDic valueForKey:key] == nil) { 
     [mutableDic setObject:[NSMutableArray array] forKey:key]; 
    } 
    [(NSMutableArray*)[mutableDic objectForKey:key] addObject:dic]; 
} 

你只需要做一次例如下載數據後。然後將這個字典保存爲一個類變量。

然後就可以訪問此字典如下:字典的

return [[[mutableDic allValues] objectAtIndex:section] count]; 

cell = [[[mutableDic allValues] objectAtIndex:section] objectAtIndex:row]; 
+0

感謝您的幫助,但如果我使用self.rows.count,在每一節中我找到相同數量的行和同一酒名 – Smola 2011-05-06 08:28:00

+0

謝謝iPortable,我用日誌驗證,節數很好,但當我這樣做:return [[[mutableDic allKeys] objectAtIndex:section] count];我有這個錯誤: - [NSCFString count]:無法識別的選擇器發送到實例 – Smola 2011-05-06 09:01:08

+0

對不起,使用allValues而不是 – 2011-05-06 10:17:35

0

你好像在這裏缺少一些代碼。字典不在任何地方定義。或者它超出了你發佈的範圍。我會建議打印字典。我想你會發現它沒有你所期望的。

NSLog(@"dict %@", dict);

+0

字典中(空隙)viewDidLoad中定義的方法。 – Smola 2011-05-06 08:16:30