-2

i have a Array of Dict , which display a Specific data into tableview ,I've been reading up on how to sort arrays of objects using the value of a property, and how to sort an array of NSDictionaries using one of their keys,

My Question is :-

how to customize header of Section in TableView and using category as a header section , see the screenshot i want to do the same thing .

(
{
    categories = Protection;
    "name_en" = "Wind deflectors, front";
    "part_number" = A4221ADE00;
    "part_pic1" = "a4221ade00/a4221ade00.jpg";
},
{
    categories = Protection;
    "name_en" = "Ice & sunscreen";
    "part_number" = A4723ADE00;
    "part_pic1" = "a4723ade00/a4723ade00.jpg";
},
{
    categories = Protection;
    "name_en" = "Carens-UN-BK-2009-AccBrochure";
    "part_number" = "Carens-UN-BK-2009-AccBrochure";
    "part_pic1" = "carens-un-bk-2009-accbrochure/carens-un-2009.jpg";
},
{
    categories = Transport;
    "name_en" = "Battery Petrol engines 2.0 GDI / Diesel engine 1.7 CRDi Carens";
    "part_number" = LP370APE070CK0;
    "part_pic1" = "b2143ade00ev/b2143ade00ev-soul2014-ev-floormat.jpg";
})

enter image description here

rmaddy
  • 314,917
  • 42
  • 532
  • 579
nivritgupta
  • 1,966
  • 2
  • 20
  • 38
  • possible duplicate of [Sorting NSDictionary from JSON for UITableView](http://stackoverflow.com/questions/10386100/sorting-nsdictionary-from-json-for-uitableview) – Michał Ciuba Dec 09 '14 at 20:24

1 Answers1

1

Not sure I entirely understand the question, but I'll take a stab. So the first step sounds like you want to sort the array of dictionaries based on the @"categories" key. This will put the array in alphabetical order based on that key:

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"categories" ascending:YES];
NSArray *sortedArray = [arrayOfDictionaries sortedArrayUsingDescriptors:@[sortDescriptor]];

Then you want the section headers of the table to be the names of the categories. First you will need to find out how many different categories there are. A method like this can get that into an array without duplicates:

+ (NSArray *)findCategoriesInArray:(NSArray *)arrayOfDictionaries
{
    NSMutableArray *categories = [NSMutableArray array];
    for (id object in arrayOfDictionaries) {
        if ([object isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dictionary = (NSDictionary *)object;
            if (dictionary[@"categories"] && ![categories containsObject:dictionary[@"categories"]]) {
                [categories addObject:dictionary[@"categories"]];
            }
        }
    }
    return categories;
}

At this point you have an array of all the dictionaries sorted by the categories key and an array containing the different categories. Let's assume the categories array from the method above gets assigned to a property self.categories (which is an NSArray). So now we can populate the section headers in the normal method calls:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.categories.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return (NSString *)self.categories[section];    // I'm assuming these are all strings
}

Finally, we can populate the rows in each section:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *headerTitle = (NSString *)self.categories[section];
    return [NameOfClass findNumberOfCategory:headerTitle inArray:self.arrayOfDictionaries];
}

+ (NSInteger)findNumberOfCategory:(NSString *)category inArray:(NSArray *)arrayOfDictionaries
{
    NSInteger number = 0;
    for (id object in arrayOfDictionaries) {
        if ([object isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dictionary = (NSDictionary *)object;
            if (dictionary[@"categories"] && [dictionary[@"categories"] isKindOfClass:[NSString class]]) {
                NSString *thisCategory = (NSString *)dictionary[@"categories"];
                if ([thisCategory isEqualToString:category]) {
                    number++;
                }
            }
        }
    }
    return number;
}

Assuming both the self.arrayOfDictionaries and the self.categories arrays are both in alphabetical order, you can populate the cells themselves in tableView:cellForRowAtIndexPath: by getting the properties of the object from self.arrayOfDictionaries[indexPath.row].

keithbhunter
  • 12,258
  • 4
  • 33
  • 58
  • what is nameoftheclass in numberOfRowsInSection ? – nivritgupta Dec 10 '14 at 10:23
  • one more Question how we can use this method + (NSArray *)findCategoriesInArray:(NSArray *)arrayOfDictionaries – nivritgupta Dec 10 '14 at 10:45
  • The two methods that start with `+` are class methods. So you don't need an instance of an object to call them unlike an instance method (method that starts with `-`). Read [link](http://stackoverflow.com/questions/1053592/what-is-the-difference-between-class-and-instance-methods) to learn a little bit more about it. Let's say the method is in a class called MyViewController.m. You would call the method like this: `self.categories = [MyViewController findCategoriesInArray:self.arrayOfDictionaries];`. – keithbhunter Dec 10 '14 at 13:41
  • Thanks its working and sorry i can not fully explain my Question but your all the methods are working – nivritgupta Dec 10 '14 at 19:01
  • one last Question how we can use this method + (NSArray *)findCategoriesInArray:(NSArray *)arrayOfDictionaries i am just using this method to Filters the category NSArray *states = [CategoryArray valueForKey:@"categories"]; NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:states]; uniqueStates = [orderedSet set]; array = [uniqueStates allObjects]; NSLog(@"%@",array); – nivritgupta Dec 10 '14 at 19:20
  • If those lines of code give you an array filtered by the categories key, then you don't need the findCategoriesInArray: method. – keithbhunter Dec 10 '14 at 19:24