私の問題を段階的に説明します:
- NSDictionariesを含むNSArrayがあります。
- 各NSDictionaryには、NSDateとNSNumber(2つのキー/ 2つの値)があります。
- UITableViewに動的セクションとセクションごとの動的行を入力したい
- UITableViewの各セクションは、週ごとに時系列に並べる必要があります(NSCalendarを使用)。
- 各行は、各セクションの下の日ごと(週ごと)に時系列である必要があります。
ここで、NSCalendarを使用して、各辞書がどの週に入るのかを調べ、このコードによって、どのセクション(週)にいくつの行(日/メッセージ)があるかを動的に調べることができました。
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
次に、1週間のインスタンスを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;
}
できれば助けてください。私は本当にあなたの助けに感謝します!ありがとうございました!