简体   繁体   中英

Count numberOfRowsInSection according section

I am creating an Ipad application which will act as wine list.

My wine list is organized in section, every section possesses a different number of row.

My problem is that I do not manage to count the number of row according the sections.

I parse this Json File:

   {"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"}]}

Normally I should have 2 rows for the section "Moulis" and one row for the others. I tried several solutions among which:

    - (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];
}

it returns

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)

It returns the good sections but not the good lines.

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

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;

    );

}

This code works perfectly, but now I would like to alphabetize my wine list. My dictionnary is good create in order alphabetical. I saw in the documentation which allKeys sort the result at random, so I use:

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

in viewForHeaderInSection, and

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

in cellForRowAtIndexPath, but wines do not correspond any more with sections.

Any kind of help is very much appreciated.

Thanks ;)

what are you exactly trying? I think it should be

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

then you get the count of the object "posts". What you are try is to get an object with the name "Moulis" (eg) but you have no object with that key. Your object contains and attribute named "appellation" which has the value of "Moulis". Your object also contains the keys nom, annee and prix. All together are one object.

// EDIT: actually this wouldn't even work because self.rows is already the "posts" object. so you have to return self.rows.count

then you can access each object in cellForRowAtIndexPath: by:

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

// EDIT2:
ok your added information is now enough to create you a working code ;) In your position I would first reorganize your data:

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];
}

you only have to do this once for example after downloading the data. Then save this dictionary as a class variable.

Then you can access this dictionary as follows:

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

and

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

You seem to be missing some code here. dict is not defined anywhere. Or it is defined out of scope from what you posted. I'd suggest printing just dict. I think you'll find that it doesn't have what you expect.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM