我将逐步解释我的问题:
- 我有一个 NSArray,里面有 NSDictionaries。
- 每个 NSDictionary 都有一个 NSDate 和一个 NSNumber(2 个键/2 个值)。
- 我想用动态部分和每部分的动态行填充 UITableView
- UITableView 的每个部分必须按时间顺序按周(使用 NSCalendar)。
- 每行必须在每个部分(按周)下按天按时间顺序排列。
现在,我已经使用 NSCalendar 来找出每个 Dictionary 进入哪一周,并且我已经设法通过这段代码动态找出我在哪些部分(周)中有多少行(天/消息):
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:NSWeekCalendarUnit fromDate:[NSDate date]];
NSMutableArray *tempArray = [NSMutableArray new];
NSNumber *weekNumber; int countDictionary = 0;
if (!_sectionsDetailed)
_sectionsDetailed = [NSMutableArray new];
if (!_sections)
_sections = [NSMutableArray new];
if (!_sectionsDictionary)
_sectionsDictionary = [NSMutableDictionary new];
for (NSDictionary *dictionary in _historyArray)
{
++countDictionary;
dateComponents = [calendar components:NSWeekCalendarUnit fromDate:[dictionary objectForKey:@"messageDate"]];
weekNumber = [NSNumber numberWithInteger:[dateComponents week]];
[_sectionsDetailed addObject:weekNumber];
DLog(@"DICTIONARY NUMBER: %i - WEEK NUMBER: %@", countDictionary, weekNumber);
}
从那我有以下输出(我有 4 个部分(周)的 9 条消息(行/天)):
DICTIONARY NUMBER: 1 - WEEK NUMBER: 5
DICTIONARY NUMBER: 2 - WEEK NUMBER: 5
DICTIONARY NUMBER: 3 - WEEK NUMBER: 5
DICTIONARY NUMBER: 4 - WEEK NUMBER: 3
DICTIONARY NUMBER: 5 - WEEK NUMBER: 3
DICTIONARY NUMBER: 6 - WEEK NUMBER: 2
DICTIONARY NUMBER: 7 - WEEK NUMBER: 2
DICTIONARY NUMBER: 8 - WEEK NUMBER: 1
DICTIONARY NUMBER: 9 - WEEK NUMBER: 1
然后,只有一周的一个实例,我使用NSBag计算部分(周) :
NSBag *arrayCounter = [NSBag bag];
for (id section in _sectionsDetailed)
{
[arrayCounter add:section];
}
for (id uniqueSection in arrayCounter.objects)
{
[tempArray addObject:uniqueSection];
[_sectionsDictionary setObject:[NSNumber numberWithInteger:[arrayCounter occurrencesOf:uniqueSection]] forKey:uniqueSection];
DLog(@"WEEK:%@, MESSAGES/WEEK:%i", uniqueSection, [arrayCounter occurrencesOf:uniqueSection]);
}
计数的输出是:
WEEK:5, MESSAGES/WEEK:3
WEEK:3, MESSAGES/WEEK:2
WEEK:1, MESSAGES/WEEK:2
WEEK:2, MESSAGES/WEEK:2
我的问题是:由于我需要填充 UITableView 的部分和行,我如何创建一个嵌套的 NSArray 或 NSMutableArray 来包含这样的方案:
completeArray:
> Week 1 (section 1) [ARRAY]:
> Message 1 (row 1 / day 1 in week 1/section1) [DICT]
> Message 2 (row 2 / day 2 in week 1/section1) [DICT]
> Week 2 (section 2) [ARRAY]:
> Message 1 (row 1 / day 1 in week 2/section2) [DICT]
> Message 2 (row 2 / day 2 in week 2/section2) [DICT]
and so on... .
.
.
我尝试使用以下代码获得所需的结果:
for (id section in _sections)
{
for (id dictionary in _historyArray)
{
dateComponents = [calendar components:NSWeekCalendarUnit fromDate:[dictionary objectForKey:@"messageDate"]];
weekNumber = [NSNumber numberWithInteger:[dateComponents week]];
if (section == weekNumber)
{
DLog(@"Section %@ - Dict %@",section, dictionary);
}
}
}
但是我一直在寻找一个解决方案来检查哪一行转到哪个部分....
上面的输出是这样的:
Section 5 - Dict {
messageDate = "2013-02-01 11:06:41 +0000";
messageNumber = 50;
}
Section 5 - Dict {
messageDate = "2013-02-01 11:06:40 +0000";
messageNumber = 1;
}
Section 5 - Dict {
messageDate = "2013-02-01 11:06:39 +0000";
messageNumber = 52;
}
Section 3 - Dict {
messageDate = "2013-01-18 09:18:47 +0000";
messageNumber = 88;
}
Section 3 - Dict {
messageDate = "2013-01-18 09:18:46 +0000";
messageNumber = 58;
}
Section 2 - Dict {
messageDate = "2013-01-11 09:18:24 +0000";
messageNumber = 45;
}
Section 2 - Dict {
messageDate = "2013-01-11 09:18:06 +0000";
messageNumber = 35;
}
Section 1 - Dict {
messageDate = "2013-01-04 09:17:40 +0000";
messageNumber = 30;
}
Section 1 - Dict {
messageDate = "2013-01-04 08:16:40 +0000";
messageNumber = 53;
}
如果可以的话,请帮助我。我将非常感谢您的帮助!谢谢!