简体   繁体   中英

Trouble using tableView numberOfRowsInSection

I have json that I'm parsing in a separate class and looping through it with:

            for( NSDictionary * game in games )
            {
                Game * teamGame = [[Game alloc] initWithJson:game];
                [teamGames addObject:teamGame];
            }

teamGames holds values like:

{
    awayteam = "Olympics 2014";
    gametime = "2014-05-08 19:00:00";
    hometeam = Finland;
},
{
    awayteam = Latvia;
    gametime = "2014-05-11 19:00:00";
    hometeam = "Olympics 2014";
},
{
    awayteam = "Canada 2013";
    gametime = "2014-07-19 19:00:00";
    hometeam = USA;
},
{
    awayteam = "USA 2014";
    gametime = "2014-01-11 15:00:00";
    hometeam = Sweden;
},
{
    awayteam = "Test 2014";
    gametime = "2013-05-16 10:00:00";
    hometeam = Sweden;
},
{
    awayteam = "Olympics 2014";
    gametime = "2014-12-16 19:00:00";
    hometeam = Sweden;
},
{
    awayteam = "Test 2014";
    gametime = "2014-01-11 15:00:00";
    hometeam = Sweden;
},

I want to fill the table headers with non repeating dates and the cell's with the corresponding team names. Same dates need to be placed in the same section. Iv'e been able to fill the table by using return 1; in numberOfRowsInSection but if theres more then one game it wont be shown.

How can I achieve this?

A dictionary of the dates and array's of games within that date?

Thanks!

In numberOfRowsInSection, simply return the number of items (count) of the array.

The value returned by this method is the number of rows that will appear in the tableview section (as described in the method title).

Re-reading your question, I think this is your solution:

1.) Create an NSMutableArray for each date containing the games for that date.

2.) As you create each array, add it to a NSMutableDictionary with the key set as the Date for that array. [dictionaryVar setObject:yourArrayOfGames forKey:@"TheDateYouWantToUseForThatArray"];

3.) In your numberOfRowsInSection method, use the section Integer (that the method already has) to get the correct array. Since you can't access a property in a dictionary with an index integer, you'll need to convert the keys in the dict to an array and then use the section integer to grab the correct key, and then finally use it on the array. Should look something like:

NSMutableArray *arrayOfDictKeys = [[NSMutableArray alloc] init];

arrayOfDictKeys = [dictionaryVar allKeys]; //(this grabs a list of all the keys in the dictionary) NSString *key = [arrayOfDictKeys objectAtIndex:section]; //(this is where you use the section var)

NSMutableArray *arrayForKey = [[NSMutableArray alloc] init];
arrayForKey = [dictionaryVar objectForKey:key];
return arrayForKey.count;

For this to work, you need to make sure the you insert the Key\\Array pairs into your dictionary in the correct order, otherwise using section grab your key will probably grab you an unexpected date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM