9

我有一个类似于以下内容的 NSArray:

6/1/13 | Data
6/2/13 | Data
7/1/13 | Data
9/1/13 | Data

我需要以某种方式获得几个月来创建节标题 - 但前提是它们在数组中,然后将日期分解为适当的部分。看起来像:

(Section Header)June 2013
6/1/13 | Data
6/2/13 | Data

(Section Header)July 2013
7/1/13 | Data

(skips august as no dates from august are in array)

(Section Header)September 2013
9/1/13 | Data

我正在尝试实施:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{
    return @"June 2013";
}

但显然需要这个来动态更新数组中的任何月份。日期实际上是数组中的 NSDates - 如果这有什么不同的话。

4

2 回答 2

22

我拼凑了一些至少应该可以编译但完全未经测试的东西。基本上,这涉及预处理您的数组并将结果存储在其他集合中,然后这些集合可以用作您的UITableViewDataSource.

将这些属性添加到作为您的数据源的类。如果您使用 ARC,则必须以不同方式声明它们。

@property(retain) NSMutableArray* tableViewSections;
@property(retain) NSMutableDictionary* tableViewCells;

将此方法添加到您的数据源并确保在UITableView调用第一个数据源方法之前的某个时间调用它。重要提示:您的数组必须包含NSDate按排序顺序的对象(您问题中的示例暗示就是这种情况)。

- (void) setupDataSource:(NSArray*)sortedDateArray
{
  self.tableViewSections = [NSMutableArray arrayWithCapacity:0];
  self.tableViewCells = [NSMutableDictionary dictionaryWithCapacity:0];

  NSCalendar* calendar = [NSCalendar currentCalendar];
  NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
  dateFormatter.locale = [NSLocale currentLocale];
  dateFormatter.timeZone = calendar.timeZone;
  [dateFormatter setDateFormat:@"MMMM YYYY"];

  NSUInteger dateComponents = NSYearCalendarUnit | NSMonthCalendarUnit;
  NSInteger previousYear = -1;
  NSInteger previousMonth = -1;
  NSMutableArray* tableViewCellsForSection = nil;
  for (NSDate* date in sortedDateArray)
  {
    NSDateComponents* components = [calendar components:dateComponents fromDate:date];
    NSInteger year = [components year];
    NSInteger month = [components month];
    if (year != previousYear || month != previousMonth)
    {
      NSString* sectionHeading = [dateFormatter stringFromDate:date];
      [self.tableViewSections addObject:sectionHeading];
      tableViewCellsForSection = [NSMutableArray arrayWithCapacity:0];
      [self.tableViewCells setObject:tableViewCellsForSection forKey:sectionHeading];
      previousYear = year;
      previousMonth = month;
    }
    [tableViewCellsForSection addObject:date];
  }
}

现在在您的数据源方法中,您可以说:

- (NSInteger) numberOfSectionsInTableView:(UITableView*)tableView
{
    return self.tableViewSections.count;
}

- (NSInteger) tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    id key = [self.tableViewSections objectAtIndex:section];
    NSArray* tableViewCellsForSection = [self.tableViewCells objectForKey:key];
    return tableViewCellsForSection.count;
}

- (NSString*) tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.tableViewSections objectAtIndex:section];
}

[...]

其余的实现留给您作为练习:-) 每当您的数组内容发生更改时,您显然需要调用setupDataSource:以更新tableViewSectionsand的内容tableViewCells

于 2013-06-26T22:29:28.343 回答
2

您需要转换现有的单个数组并创建一个新的字典数组。这个新数组中的每个字典都将包含两个条目 - 一个用于月份,另一个条目将是一个数组,其中包含与月份相关联的每一行的数据。

如果您需要向此结构添加新行,请参阅列表中的月份。如果是这样,请更新该月的数组。否则,创建一个包含新月份的新字典和一个包含一个新行的新数组。

于 2013-06-26T21:42:00.743 回答