2011-05-06 79 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