UITableView Section按标题中的日期月份标题 [英] UITableView Section Headers by month of dates in array

查看:103
本文介绍了UITableView Section按标题中的日期月份标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an NSArray with something similar to:

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

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

What I need to somehow get the months to create section headers - but only if they are in the array and then break the dates up into the appropriate sections. Looking like:

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

But obviously need this to dynamically update with whatever months are in the array. The dates are actually NSDates that are in the array - if that makes any difference.

推荐答案

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

I have cobbled together something that should at least compile, but which is completely untested. Basically this involves pre-processing your array and storing the results in other collections that can then serve as model objects for your UITableViewDataSource.

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

Add these properties to the class that is your data source. You have to declare them differently if you are using ARC.

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

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

Add this method to your data source and make sure that you invoke it at some time before UITableView invokes your first data source method. Important: Your array must contain the NSDate objects in sorted order (the example in your question implies that this is the case).

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

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

Now in your data source methods you can say:

- (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 tableViewCells

The rest of the implementation is left as an exercise to you :-) Whenever the content of your array changes you obviously need to invoke setupDataSource: to update the contents of tableViewSections and tableViewCells.

这篇关于UITableView Section按标题中的日期月份标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆