iOS Using NSDictionary to load data into section and rows

若如初见. 提交于 2019-12-01 01:08:39

To process your data you would need to do something like

NSMutableDictionary *dataSource = [[NSMutableDictionary alloc] init]; // This would need to be an ivar

for (NSDictionary *rawItem in rawItems) {
    NSString *date = [rawItem objectForKey:@"date"]; // Store in the dictionary using the data as the key

    NSMutableArray *section = [dataSource objectForKey:date]; // Grab the section that corresponds to the date

    if (!section) { // If there is no section then create one and add it to the dataSource
        section = [[NSMutableArray alloc] init];
        [dataSource setObject:section forKey:date];
    }

    [section addObject:rawItem]; // add your object
}

self.dataSource = dataSource;

Then to get the section title

NSArray *sections =[[self.dataSource allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
return [sections objectAtIndexPath:indexPath.section];

Then to get a row in a section

NSString *sectionTitle = // get the section title calling the above code
NSArray *items = [self.dataSource objectForKey:sectionTitle];

return [items objectAtIndex:indexPath.row];
 - Dictionary of sections by date
  |
   --- array of row objects for the date1
  |    |
  |     --- row dictionary with the data to display
  |    |
  |     --- row dictionary with the data to display
  |    |
  |     --- row dictionary with the data to display
  |    |
  |     --- row dictionary with the data to display
  |
  |
  |
   --- array of row objects for the date2
  |    |
  |     --- row dictionary with the data to display
  |    |
  |     --- row dictionary with the data to display
  |    |
  |     --- row dictionary with the data to display
  |
  |
  |
  |
   --- array of row objects for the date3
       |
        --- row dictionary with the data to display
       |
        --- row dictionary with the data to display
       |
        --- row dictionary with the data to display
       |
        --- row dictionary with the data to display
       |
        --- row dictionary with the data to display

In short, you can't re-add objects to an NSDictionary. That is because NSDictionary is immutable. Once it has been created, it's contents can not be modified. What you want is an NSMutableDictionary.

You can then use the setObject: forKey: method.

[MyDict setObject:@"Normal Loan" forKey:@"status"];

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsmutabledictionary_Class/Reference/Reference.html

Intentss

As A-Live said you are going to have to sort your Array in to a Mutable Dictionary of Arrays

The following code should work, but it's untested

In TableView Controller .h

@interface MyController:UITableViewController{
   NSMutableArray* dict;
   NSMutableArray* array;
}

In MyController.m

-(void)initWithBooksArray:(NSArray*)bookArray{ //Or something similar

    self = [[super alloc] initWithStyle:UITableViewStylePlain];
    if(self){        
       dict = [[NSMutableDictionary alloc] init];
       array= [[NSMutableArray alloc] init];

       NSMutableArray* sectionArray;
       for(NSDictionary* bookDict in bookArray){

          date = [bookDict valueForKey:@"date"];
          sectionArray = [dict valueForKey:date];
          if(sectionArray){
             [sectionArray addObject:bookDict];
          }else{
             sectionArray = [[NSMutableArray arrayWithObject:bookDict];
             [dict addObject:sectionArray forKey:date];
          }
       }
       array = [[dict allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

    }
    return self;
}

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

-(NSInterger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
   NSString* key = [array objectAtIndex:section];
   return [[dict objectForKey:key]count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
   return [array objectAtIndex:section];
}

...

// I'll leave it to apple's documentation (or someone else) to fill in the blanks on how to create cells

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!