use*_*561 9 objective-c uitableview nsarray ios
我有一个类似于以下内容的NSArray:
6/1/13 | Data
6/2/13 | Data
7/1/13 | Data
9/1/13 | Data
Run Code Online (Sandbox Code Playgroud)
我需要以某种方式获得创建节标题的月份 - 但前提是它们在数组中然后将日期分解为适当的部分.看起来像:
(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
Run Code Online (Sandbox Code Playgroud)
我试图实现:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"June 2013";
}
Run Code Online (Sandbox Code Playgroud)
但显然需要使用数组中的任何月份动态更新.日期实际上是数组中的NSDates - 如果这有任何区别.
her*_*ube 22
我拼凑了至少应该编译的东西,但是完全未经测试.基本上这涉及预处理您的数组并将结果存储在其他集合中,然后可以作为您的模型对象UITableViewDataSource.
将这些属性添加到作为数据源的类中.如果使用ARC,则必须以不同方式声明它们.
@property(retain) NSMutableArray* tableViewSections;
@property(retain) NSMutableDictionary* tableViewCells;
Run Code Online (Sandbox Code Playgroud)
将此方法添加到数据源中,并确保在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];
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在您的数据源方法中,您可以说:
- (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];
}
[...]
Run Code Online (Sandbox Code Playgroud)
实施的其余部分留作一个练习,你:-)每当你的阵列的内容变化,你显然需要调用setupDataSource:更新的内容tableViewSections和tableViewCells.
| 归档时间: |
|
| 查看次数: |
7221 次 |
| 最近记录: |