Ios json分组日期和填写uiTableView标题
我有json数据,我想按日期为节头分组,并用相应的团队名称填充表视图的节 数据: Iv'e使用另一个类解析它,并拥有一个包含所有内容的数组(teamGames): 如何用非重复日期及其对应的游戏名称填充表格标题 谢谢 更新: 一个日期内可能会有不止一场比赛。问题在numberOfRowsInSection中。我不知道如何创建包含日期和相应游戏的数组 表视图标题方法:Ios json分组日期和填写uiTableView标题,ios,json,uitableview,ios7,Ios,Json,Uitableview,Ios7,我有json数据,我想按日期为节头分组,并用相应的团队名称填充表视图的节 数据: Iv'e使用另一个类解析它,并拥有一个包含所有内容的数组(teamGames): 如何用非重复日期及其对应的游戏名称填充表格标题 谢谢 更新: 一个日期内可能会有不止一场比赛。问题在numberOfRowsInSection中。我不知道如何创建包含日期和相应游戏的数组 表视图标题方法: - (NSString *)tableView:(UITableView *)tableView titleForHeaderIn
- (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]];
}
假设数据是可以访问文件中任何位置的数组,如属性或实例变量,则只需执行以下操作:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSDictionary *team = data[section];
return [NSString stringWithFormat:@"%@ vs. %@, Date: %@", team[@"hometeam"], team[@"awayteam"], team[@"gametime"]];
}
请参阅更新。我已经把日期格式化了。如果一天中有超过一场比赛,他们必须在同一部分中展示。。。问题是numberOfRowsInSection没有返回我不知道如何创建的数组。在这种情况下,您可能需要重新考虑如何格式化JSON。也许游戏对象保存在每个日期的游戏数组中?
- (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]];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSDictionary *team = data[section];
return [NSString stringWithFormat:@"%@ vs. %@, Date: %@", team[@"hometeam"], team[@"awayteam"], team[@"gametime"]];
}