Search code examples
iosjsonuitableviewios7

json grouping date and filling uiTableView headers


I have json data that I want to group by dates for the section headers and fill the sections of the table view with the corresponding team names.

data:

[
{
    "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"
}
]

Iv'e parse it using another class and have an array containing everything (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];
            }
         }

How can I fill the table headers with non repeating dates and their corresponding game names?

Thanks!

update:

There maybe more then one game within a date. the problem is in numberOfRowsInSection. I dont know how to create that array containing dates with corresponding games within it.

table view header method:

- (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]];
}

Solution

  • Assuming data is an array you can access anywhere in the file, like a property or instance variable, all you need to do is something like this:

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