2

我从服务器获取 json 并将它们添加到 NSMutableArray 中,如下所示:

{
  "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",
}

如何按“日期时间”使用 NSMutableArray 组并在 uitableview 中显示?

4

1 回答 1

0

您可以为此滚动您自己的数据结构,但是当您指定 a 时, TLIndexPathTools 库会sectionNameKeyPath自动对您的表进行分区,在这种情况下,它只是“日期时间”。我在 GitHub 上放了一个关于你的数据的工作示例。尝试运行JSON 示例项目。视图控制器的完整源码如下:

#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
于 2013-06-07T01:54:21.553 回答