tags:

views:

33

answers:

1

i am pulling data from xml so is that possible to show them group by??

i have no plsit right now i am showing data just as simple so now i want group by what should be the approach/concept??

i did something like this but no luck

d*o i need to do some sorting?? like using NSSortDescriptor??*

if yes then where in viewdidload or in cellforrowatindxpath??

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //UITableView *tableView;
    UITableViewCell *cell;
    static NSString *CellIdentifier = @"Cell";


    **Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];**

/// how to arrange them in group by??

i tried this

NSString *keys =aBook.name;
NSMutableDictionary *dicta = [NSMutableDictionary dictionary];
    [dicta setObject:keys forKey:@"message"];


    NSSortDescriptor *sortNameDescriptor = 
    [[[NSSortDescriptor alloc] 
      initWithKey:@"message" ascending:YES] 
     autorelease];

but i cant display them in alphabetical order :(

}

Thanks

A: 

Most of the time I create a NSDictionary where each entry has a key containing the sectionName and the value is a list of anything used to fill the cell. If you have that :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 return [[data allkeys] count]; 
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  NSString* key = [[data allkeys] objectAtIndex:section];
  return [[data objectForKey:key] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  return [[data allkeys] objectAtIndex:section]; //If key is NSString* of course
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  ...//classical code the retrieve a valid cell
  NSString* key = [[data allKeys] objectAtIndex:indexPath.section];
  NSObject* obj = [[data objectForKey:key] objectAtIndex:indexPath.row];
  ...//Setup the cell with obj properties.
}

You can optimize that code a lot but it works fine for small amount of data.

if you want any of the group or value to be sorted, you can use NSSortDescriptors like this for exemple (sort an array of string)

NSSortDescriptor* sortAsc = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
[anyArrayOfNSString sortUsingDescriptors:[NSArray arrayWithObject:sortAsc]];        
[sortAsc release];

of course I advice you to avoid sorting every time you need a value. Do the sort once when the data becomes available.

VdesmedT
you mean this way i can arrange them in alphabetical order???
prajakta
@arutema : see post modification above.
VdesmedT
ok 1 doubt .. what is issueNumbers?? (an array) and initWithKey: should be nil or my key?? see my modified code above
prajakta
i am just confused where to sort , should i sort when i am parsing xml or when ???
prajakta
Of course the best moment to arrange your NSArray's is when you parse your xml or just after. Be aware that a dictionary is never sorted but that you can keep a sorted Array of its key.
VdesmedT
Il you arrays is an array of NSString, sort key must be null ! The key is used to provide a "path" to the "property" to use when comparing elements.
VdesmedT