如何将NSMutableArray分组并在UITableView中显示? [英] How to group an NSMutableArray and show in UITableView?
本文介绍了如何将NSMutableArray分组并在UITableView中显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从服务器获取json并将其添加到NSMutableArray中,如下所示:
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",
}
如何在"datetime"之前使用NSMutableArray组并在uitableview中显示?
How can I use the NSMutableArray group by "datetime" and show in uitableview?
推荐答案
您可以为此滚动自己的数据结构,但 JSON示例项目.视图控制器的完整源代码如下:
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
这篇关于如何将NSMutableArray分组并在UITableView中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文