I get json from server and add them in NSMutableArray like this:
{
"title": "title60",
"summary": "summary60",
"datetime": "2013.02.03",
}
{
"id": 58,
"title": "title59",
"summary": "summary59",
"datetime": "2013.02.03",
},
{
"id": 57,
"title": "title58",
"summary": "summary58",
"datetime": "2013.02.04",
},
{
"id": 56,
"title": "title57",
"summary": "summary57",
"datetime": "2013.02.04",
},
{
"id": 55,
"title": "title56",
"summary": "summary56",
"datetime": "2013.02.05",
}
How can I use the NSMutableArray group by "datetime" and show in uitableview?
You can roll your own data structures for this, but the TLIndexPathTools library automatically sections your table when you specify a sectionNameKeyPath
, which in this case is just going to be "datetime". I put a working example with your data up on GitHub. Try running the JSON example project. The full source of the view controller is as follows:
#import "TLTableViewController.h"
@interface JSONTableViewController : TLTableViewController
@end
#import "JSONTableViewController.h"
#import "TLIndexPathDataModel.h"
@implementation JSONTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//simulated JSON response as an array of dictionaries
NSArray *jsonData = @[
@{
@"id": @(58),
@"title": @"title59",
@"summary": @"summary59",
@"datetime": @"2013.02.03",
},
@{
@"id": @(57),
@"title": @"title58",
@"summary": @"summary58",
@"datetime": @"2013.02.04",
},
@{
@"id": @(56),
@"title": @"title57",
@"summary": @"summary57",
@"datetime": @"2013.02.04",
},
@{
@"id": @(55),
@"title": @"title56",
@"summary": @"summary56",
@"datetime": @"2013.02.05",
}
];
//initialize index path controller with a data model containing JSON data.
//using "datetime" as the `sectionNameKeyPath` automatically groups items
//by "datetime".
self.indexPathController.dataModel = [[TLIndexPathDataModel alloc] initWithItems:jsonData
andSectionNameKeyPath:@"datetime"
andIdentifierKeyPath:@"id"];
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dict = [self.indexPathController.dataModel itemAtIndexPath:indexPath];
cell.textLabel.text = dict[@"title"];
cell.detailTextLabel.text = dict[@"summary"];
}
@end