json 分组日期和填充 uiTableView 标题

Posted

技术标签:

【中文标题】json 分组日期和填充 uiTableView 标题【英文标题】:json grouping date and filling uiTableView headers 【发布时间】:2014-08-25 17:46:23 【问题描述】:

我有 json 数据,我想按日期对部分标题进行分组,并用相应的团队名称填充表格视图的部分。

数据:

[

    "awayscore": null,
    "awayteam": "Norway",
    "collectionid": "33958",
    "gameid": null,
    "gametime": "2014-05-03 19:00:00",
    "homescore": null,
    "hometeam": "Olympics 2014",
    "id": "20921",
    "opplogo": null,
    "psiteamid": "0",
    "seasonid": "166",
    "side": "home",
    "status": null,
    "teamid": "20016"
,

    "awayscore": null,
    "awayteam": "Austria",
    "collectionid": "33959",
    "gameid": null,
    "gametime": "2014-05-03 19:00:00",
    "homescore": null,
    "hometeam": "Olympics 2014",
    "id": "20922",
    "opplogo": null,
    "psiteamid": "0",
    "seasonid": "166",
    "side": "home",
    "status": null,
    "teamid": "20016"
,

    "awayscore": null,
    "awayteam": "Olympics 2014",
    "collectionid": "33960",
    "gameid": null,
    "gametime": "2014-05-03 19:00:00",
    "homescore": null,
    "hometeam": "Finland",
    "id": "20923",
    "opplogo": null,
    "psiteamid": "0",
    "seasonid": "166",
    "side": "away",
    "status": null,
    "teamid": "20016"
,

    "awayscore": null,
    "awayteam": "Latvia",
    "collectionid": "33961",
    "gameid": null,
    "gametime": "2014-05-11 19:00:00",
    "homescore": null,
    "hometeam": "Olympics 2014",
    "id": "20924",
    "opplogo": null,
    "psiteamid": "0",
    "seasonid": "166",
    "side": "home",
    "status": null,
    "teamid": "20016"
,

    "awayscore": null,
    "awayteam": "Olympics 2014",
    "collectionid": "33962",
    "gameid": "20925",
    "gametime": "2014-05-13 19:00:00",
    "homescore": null,
    "hometeam": "USA",
    "id": "20925",
    "opplogo": null,
    "psiteamid": "0",
    "seasonid": "166",
    "side": "away",
    "status": null,
    "teamid": "20016"
,

    "awayscore": null,
    "awayteam": "Olympics 2014",
    "collectionid": "33963",
    "gameid": null,
    "gametime": "2014-05-16 19:00:00",
    "homescore": null,
    "hometeam": "Sweden",
    "id": "20926",
    "opplogo": null,
    "psiteamid": "0",
    "seasonid": "166",
    "side": "away",
    "status": null,
    "teamid": "20016"
,

    "awayscore": "2",
    "awayteam": "Dolphins",
    "collectionid": "115563",
    "gameid": null,
    "gametime": "2014-07-16 00:00:00",
    "homescore": "4",
    "hometeam": "Whales",
    "id": "21914",
    "opplogo": null,
    "psiteamid": "0",
    "seasonid": "166",
    "side": "home",
    "status": "COMPLETE",
    "teamid": "20016"
,

    "awayscore": "0",
    "awayteam": "Dolphins Quicker",
    "collectionid": "115564",
    "gameid": null,
    "gametime": "2014-07-24 00:00:00",
    "homescore": "3",
    "hometeam": "Whales 2",
    "id": "21915",
    "opplogo": null,
    "psiteamid": "0",
    "seasonid": "166",
    "side": "home",
    "status": "COMPLETE",
    "teamid": "20016"

]

我使用另一个类解析它并拥有一个包含所有内容的数组(teamGames):

       NSArray * games             = [NSJSONSerialization
                                       JSONObjectWithData:data
                                       options:kNilOptions error:&jsonParseError];

        DDLogVerbose(@"seasons results: %@", games);

        if (jsonParseError) 
            UIAlertView * jsonParseErrorAlert = [[UIAlertView alloc]
                                                 initWithTitle:@"Error"
                                                 message:@"Error with jsonParseErrorAlert"
                                                 delegate:nil
                                                 cancelButtonTitle:@"OK"
                                                 otherButtonTitles:nil];
            [jsonParseErrorAlert show];
        
        else
        
            for( NSDictionary * game in games )
            
                Game * teamGame = [[Game alloc] initWithJson:game];
                [teamGames addObject:teamGame];
            
         

如何在表格标题中填写不重复的日期及其对应的游戏名称?

谢谢!

更新:

一个日期内可能有不止一场比赛。问题出在 numberOfRowsInSection 中。我不知道如何创建包含日期的数组,其中包含相应的游戏。

表格视图标题方法:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

Game * game = [teamGames objectAtIndex:section];
NSString        * str           = game.gametimeStr;
NSDateFormatter * dateFormat    = [[NSDateFormatter alloc] init];
NSLocale        * usLocale      = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

[dateFormat setLocale:usLocale];
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

NSDate * dte = [dateFormat dateFromString:str];
[dateFormat setDateFormat:@"MMM dd"];

return [NSString stringWithFormat:@"%@",[dateFormat stringFromDate:dte]];

【问题讨论】:

【参考方案1】:

假设 data 是一个数组,您可以在文件中的任何位置访问,例如属性或实例变量,您需要做的就是这样:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
    NSDictionary *team = data[section];
    return [NSString stringWithFormat:@"%@ vs. %@, Date: %@", team[@"hometeam"], team[@"awayteam"], team[@"gametime"]];

【讨论】:

查看更新。我已经格式化了日期。如果一天中有超过 1 场比赛,他们必须在同一部分显示......问题是 numberOfRowsInSection 没有返回我不知道如何创建的数组 您可能需要重新考虑在这种情况下如何格式化 JSON。也许每个日期的游戏对象都保存在游戏数组中?

以上是关于json 分组日期和填充 uiTableView 标题的主要内容,如果未能解决你的问题,请参考以下文章

分组 UITableView 35 点/像素顶部插入/填充

用部分动态填充分组的 uitableView

UITableView 中的 UITableView,填充了字符串和数组的 JSON 数组 [关闭]

如何使用 JSON 数据填充 UITableView?

如何修复 JSON 不填充我的 UITableView?

如何使用来自不同 ViewController 的 JSON 响应填充 UITableView?