【问题标题】:UITableView Section Headers by month of dates in arrayUITableView 节标题按数组中的日期月份
【发布时间】:2013-06-26 21:09:58
【问题描述】:

我有一个类似于以下内容的 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 - 如果这有什么不同的话。

【问题讨论】:

  • 数组项,是字符串、字典还是自定义对象?或者您想要详细说明该怎么做?

标签: ios objective-c uitableview nsarray


【解决方案1】:

我拼凑了一些至少应该可以编译但完全未经测试的东西。基本上,这涉及预处理您的数组并将结果存储在其他集合中,然后这些集合可以用作您的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: 来更新 tableViewSectionstableViewCells 的内容。

【讨论】:

  • 是的 - 您正确地假设了正在排序的数组。这正是我想要的,并且能够相应地修改代码。谢谢!
  • @user1453561 让我知道代码是否包含任何错误,我会相应地编辑答案,以便对其他人有用。如果您只需要编辑内容以使其适应您的需求,则无需采取行动。
  • 我唯一注意到的是 ` [self.tableViewCells setObject:tableViewCellsForSection forKey:sectionHeading];` 应该是[self.tableViewCells setValue:tableViewCellsForSection forKey:sectionHeading]; 其他一切看起来都很好!
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-04
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    • 2018-08-30
    相关资源
    最近更新 更多