我有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]];
}
答案 0 :(得分:0)
假设数据是一个数组,您可以访问文件中的任何位置,例如属性或实例变量,您需要做的就是这样:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSDictionary *team = data[section];
return [NSString stringWithFormat:@"%@ vs. %@, Date: %@", team[@"hometeam"], team[@"awayteam"], team[@"gametime"]];
}