1

我将逐步解释我的问题:

  1. 我有一个 NSArray,里面有 NSDictionaries。
  2. 每个 NSDictionary 都有一个 NSDate 和一个 NSNumber(2 个键/2 个值)。
  3. 我想用动态部分和每部分的动态行填充 UITableView
  4. UITableView 的每个部分必须按时间顺序按周(使用 NSCalendar)。
  5. 每行必须在每个部分(按周)下按天按时间顺序排列。

现在,我已经使用 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;
}

如果可以的话,请帮助我。我将非常感谢您的帮助!谢谢!

4

2 回答 2

1

好的,所以您现在拥有数据结构的方式使您想要做的事情几乎不可能。事实上,这里有很多更简单的方法来做这些事情。

因此,不要将所有这些字典(如果我们很挑剔,真的应该将其制成一个消息类)保存在一个数组中,而是尝试按照键为“Week #”和值是数组的行创建一个 NSDictionary消息(或在本例中为字典)。

然后填充 TableView,numberOfSections = [[aDictionary allKeys] count]... 然后枚举这些部分。每个部分的 numberOfRows = [[aDictionary objectForKey: aSection] count],这将为您提供一周的消息数组,然后您可以在数组上枚举。(希望这一点得到了理解……对不起,我很草率)

您遇到的问题是可以通过使用正确类型的数据结构轻松避免的问题,TableView 是一个二维数组,您试图单独填充它的每个元素,而不是匹配刚刚制作的数据结构很头疼,你可能知道哈哈

于 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 回答