Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 19th, 2012  |  syntax: Objective C  |  size: 0.98 KB  |  views: 39  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.                 NSMutableArray *tableSections = [[NSMutableArray alloc] init];
  2.                 NSMutableArray *sectionData = [[NSMutableArray alloc] init];
  3.                 NSMutableArray *yourCustomDataObject;
  4.                
  5.                 int sectionIndex;
  6.                
  7.                 // Pseudo-code to create data structure
  8.                 for(id episode in episodes) {
  9.                         sectionIndex = [[episode objectForKey:@"Season"] intValue];
  10.                        
  11.                         yourCustomDataObject = [[NSMutableArray alloc] initWithObjects:episode, nil];
  12.                        
  13.                         // Do index check first to insure no out of bounds
  14.                         if(sectionIndex <= [tableSections count]) { // tableSections will always count 0
  15.                                 sectionData = [tableSections objectAtIndex:sectionIndex];
  16.                         }
  17.                        
  18.                         // Create the new section array if there isn't one for the current section
  19.                         if(!sectionData) {
  20.                                 sectionData = [NSMutableArray new];
  21.                                 [tableSections insertObject:sectionData atIndex:sectionIndex];
  22.                         }
  23.                        
  24.                         [sectionData addObject:yourCustomDataObject];
  25.                 }
  26.                
  27.                 NSLog(@"%d %d", [tableSections count], [sectionData count]);