我有一个包含足球比赛装置的数组:
[fixtures addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:date,@"date", time, @"time", competition_id,@"competition_id", home, @"home", away, @"away", nil]];
然后我有一个数组,其中包含所有没有重复的日期。这个数组也是这些部分的数组。
[sections addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:date,@"date", nil]];
我的问题是,numberOfRowsInSection& cellForRowAtIndexPath看看所有日期等于日期的灯具,然后在各个部分下显示?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
for (int i = 0; i <= sections.count-1; i++)
{
NSArray *filteredArray = [fixtures filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"date == %@",sections[i][@"date"]]];
if(section==i) {
return filteredArray.count;
}
}
}
答案 0 :(得分:0)
一种可行的解决方法,使fixtures
成为NSMutableDictionary
,其密钥类似于date
中section
的值。
然后尝试这样:(伪送)
// Inside cell for row at index path delegate
{
NSMutableDictionary *fixtureForSectionDate = [fixtures valueForKey:[[sections allKeys] objectAtIndex:indexPath.section]];
// do stuff with fixture data
}
使用相同的方法获取行数numberOfRowsInSection
。希望它有意义。
答案 1 :(得分:0)
正如我所理解的那样 - 您需要通过方便的方式从fixtures
过滤sections
数组中的日期。如果i
是部分编号 - sections[i][@"date"]
。
以下代码将执行此操作:
NSArray *filteredArray = [fixtures filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"date == %@",sections[i][@"date"]]];
NSLog(@"count = %lul", (unsigned long)filteredArray.count); // Number of items matching
NSLog([filteredArray description]); // Matching items
更新:
numberOfRowsInSection方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *filteredArray = [fixtures filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"date == %@",sections[section][@"date"]]];
return filteredArray.count;
}