UITableView Section按标题中的日期月份标题

时间:2013-06-26 21:09:59

标签: ios objective-c uitableview nsarray

我有一个类似于:

的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 - 如果这有任何区别。

2 个答案:

答案 0 :(得分: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:来更新tableViewSections和{{1}的内容}。

答案 1 :(得分:2)

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

如果您需要向此结构添加新行,请查看月份已在列表中。如果是这样,请更新该月份的数组。否则,使用新月创建一个新字典,并创建一个包含新行的新数组。