简体   繁体   English

使用tableView numberOfRowsInSection时遇到问题

[英]Trouble using tableView numberOfRowsInSection

I have json that I'm parsing in a separate class and looping through it with: 我有一个json,我在一个单独的类中进行解析,并使用以下命令循环遍历:

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

teamGames holds values like: teamGames具有以下值:

{
    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; 我可以使用return 1;来填写表格return 1; in numberOfRowsInSection but if theres more then one game it wont be shown. numberOfRowsInSection但是如果有更多游戏,则不会显示。

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. 在numberOfRowsInSection中,只需返回数组的项目数(计数)。

The value returned by this method is the number of rows that will appear in the tableview section (as described in the method title). 此方法返回的值是将出现在tableview部分中的行数(如方法标题中所述)。

Re-reading your question, I think this is your solution: 重新阅读您的问题,我认为这是您的解决方案:

1.) Create an NSMutableArray for each date containing the games for that date. 1.)为每个日期创建一个NSMutableArray,其中包含该日期的游戏。

2.) As you create each array, add it to a NSMutableDictionary with the key set as the Date for that array. 2)创建每个数组时,将其添加到NSMutableDictionary中,并将键设置为该数组的Date。 [dictionaryVar setObject:yourArrayOfGames forKey:@"TheDateYouWantToUseForThatArray"]; [dictionaryVar setObject:yourArrayOfGames forKey:@“ TheDateYouWantToUseForThatArray”];

3.) In your numberOfRowsInSection method, use the section Integer (that the method already has) to get the correct array. 3)在您的numberOfRowsInSection方法中,使用Integer section (该方法已经具有)来获取正确的数组。 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. 由于您无法使用索引整数访问字典中的属性,因此需要将dict中的键转换为数组,然后使用整数部分来获取正确的键,然后最终在数组上使用它。 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]; //(这将获取字典中所有键的列表) NSString *key = [arrayOfDictKeys objectAtIndex:section]; //(this is where you use the section var) //(这是您使用var section

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. 为此,您需要确保以正确的顺序将“ Key \\ Array”对插入字典中,否则使用“ section抓关键”可能会抓到您意外的日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在numberOfRowsInSection中调用TableView BeginUpdates - Calling TableView BeginUpdates in numberOfRowsInSection tableView中的cellForRowAtIndexPath和numberOfRowsInSection冲突 - cellForRowAtIndexPath and numberOfRowsInSection conflicting in tableView [tableView numberOfRowsInSection:0始终返回0 - [tableView numberOfRowsInSection:0 always returns 0 什么时候是tableView:numberOfRowsInSection:在UITableView中调用? - When is tableView:numberOfRowsInSection: called in UITableView? 对成员“tableView(_:numberOfRowsInSection:)”的不明确引用 - Ambiguous reference to member 'tableView(_:numberOfRowsInSection:)' 搜索时发生tableView:numberOfRowsInSection错误 - tableView:numberOfRowsInSection error when searching RxSwift TableView - 如何设置 numberOfRowsInSection? - RxSwift TableView - How to set numberOfRowsInSection? tableView:numberOfRowsInSection:连续调用两次 - tableView:numberOfRowsInSection: called twice in a row NSInvalidArgumentException tableView:numberOfRowsInSection错误…但是我没有使用选项卡式视图,也没有使用过Interface Builder - NSInvalidArgumentException tableView:numberOfRowsInSection error…but I am not using a tabbed view and I haven't used the Interface Builder at all 根据字典数组为tableview设置numberOfRowsInSection - Set numberOfRowsInSection for tableview as per the Array of Dictionaries
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM