1

私の問題を段階的に説明します:

  1. NSDictionariesを含むNSArrayがあります。
  2. 各NSDictionaryには、NSDateとNSNumber(2つのキー/ 2つの値)があります。
  3. UITableViewに動的セクションとセクションごとの動的行を入力したい
  4. UITableViewの各セクションは、週ごとに時系列に並べる必要があります(NSCalendarを使用)。
  5. 各行は、各セクションの下の日ごと(週ごと)に時系列である必要があります。

ここで、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;
}

できれば助けてください。私は本当にあなたの助けに感謝します!ありがとうございました!

4

2 に答える 2

1

さて、あなたが今データ構造を持っている方法はあなたがしたいことをほとんど不可能にします。実際、ここで行われていることを行うためのより簡単な方法はたくさんあります。

したがって、これらすべての辞書(うるさい場合は実際にメッセージクラスにする必要があります)を配列に保持する代わりに、キーの行が「週#」で値が配列であるNSDictionaryを作成してみてください。メッセージ(またはこの場合は辞書)。

したがって、TableViewにデータを入力するには、numberOfSections = [[aDictionary allKeys]count]...次にセクションを列挙します。また、各セクションのnumberOfRows = [[aDictionary objectForKey:aSection] count]の場合、これによりその週のメッセージの配列が得られ、配列を列挙するだけで済みます。(要点が伝わったことを願っています...私の側ではかなりずさんな申し訳ありません)

発生している問題は、適切な種類のデータ構造を使用することで簡単に回避できる問題です。TableViewは2次元配列であり、データ構造を一致させるのではなく、各要素を1つずつ埋めようとしています。あなたはおそらくハハを知っているので、大きな頭痛

于 2013-01-06T02:14:06.220 に答える
0

回答が遅れて申し訳ありませんが(時間がありませんでした)、次の解決策を考え出しました。ジェレミーの助けに感謝します。それはあなたの助けを借りて行われました。

- (void) archiveRecord
{
    NSDate *currentDate = [NSDate date];
    NSNumber *currentMessageNumber = [NSNumber numberWithInt:_generatedNumber];

    NSDictionary *currentMessageDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:currentMessageNumber,@"messageNumber", currentDate,@"messageDate", nil];    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *currentMessageDateComponents;
    NSDateComponents *historyMessageDateComponents;
    currentWeekNumber = 0;
    historyWeekNumber = 0;

    currentMessageDateComponents = [calendar components:NSWeekCalendarUnit fromDate:currentDate];
    currentWeekNumber = [currentMessageDateComponents week];

    if (historyExists)
    {
      for (NSMutableArray *sectionArray in [_messageHistory objectAtIndex:0])
      {
          DLog(@"SECTIONARRAY: %@", sectionArray);
          DLog(@"DICTIONARY NUMBER: %@", [sectionArray valueForKey:@"messageNumber"]);
          historyMessageDateComponents = [calendar components:NSWeekCalendarUnit fromDate:[sectionArray valueForKey:@"messageDate"]];
          historyWeekNumber = [historyMessageDateComponents week];

          if (historyWeekNumber == currentWeekNumber)
          {
              DLog(@"ADD MESSAGE TO CURRENT WEEK");
              [[_messageHistory objectAtIndex:0] insertObject:currentMessageDictionary atIndex:0];
              break;
          }
          else
          {
              DLog(@"NEW SECTION NEEDED: CREATE NEW SECTION AND ADD MESSAGE");
              [_messageHistory insertObject:[NSMutableArray arrayWithObjects:currentMessageDictionary, nil] atIndex:0];
              break;
          }
       }
    }
    else
    {
        DLog(@"CREATE NEW SECTION AND ADD MESSAGE");
        [_messageHistory addObject:[NSMutableArray arrayWithObjects:currentMessageDictionary, nil]];
    }
    DLog(@"CURRENT HISTORY: %@", _messageHistory);
    [self performSelector:@selector(writeRecordToFile)];
}
于 2013-01-11T21:43:14.700 に答える