如何将NSMutableArray分组并在UITableView中显示?
我从服务器获取json并将其添加到NSMutableArray中,如下所示:如何将NSMutableArray分组并在UITableView中显示?,uitableview,nsmutablearray,grouping,Uitableview,Nsmutablearray,Grouping,我从服务器获取json并将其添加到NSMutableArray中,如下所示: { "title": "title60", "summary": "summary60", "datetime": "2013.02.03", } { "id": 58, "title": "title59", "summary": "summary59", "datetime": "2013.02.03", }, { "id": 57, "title": "title58",
{
"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中显示?您可以为此滚动您自己的数据结构,但当您指定一个
sectionNameKeyPath
时,会自动对表进行分区,在本例中,该值仅为“datetime”。我在GitHub上提供了一个工作示例,其中包含您的数据。试着运行。视图控制器的完整源代码如下所示:
#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
“按日期时间分组”是指您希望为每个唯一的日期时间值设置一个部分吗?是的,谢谢。请给我一些建议。