为UITableView动态嵌套NSMutableArrays或NSArrays

时间:2013-01-06 00:31:31

标签: objective-c ios uitableview nsmutablearray nsarray

我将按步骤解释我的问题:

  1. 我的NSArray里面有NSDictionaries。
  2. 每个NSDictionary都有一个NSDate和一个NSNumber(2个键/ 2个值)。
  3. 我想用动态部分和动态行每个部分
  4. 填充UITableView
  5. UITableView的每个部分必须按周时间顺序排列(使用NSCalendar)。
  6. 每一行的每一行必须按时间顺序排列(按周)。
  7. 现在,我已经使用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的Sections和Rows,如何创建一个嵌套的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;
    }
    

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

2 个答案:

答案 0 :(得分:1)

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

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

然后填充TableView,numberOfSections = [[aDictionary allKeys] count] ...然后枚举这些部分。并且每个部分的numberOfRows = [[aDictionary objectForKey:aSection] count],这将为您提供本周的消息数组,然后您可以枚举数组。 (希望这一点得到解决......对我而言非常草率抱歉)

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

答案 1 :(得分:0)

对于迟到的回答(我没有时间)抱歉,我想出了以下解决方案: 谢谢Jeremy的帮助!这是在你的帮助下完成的。

- (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)];
}