Ios 从日期数组创建年份的节
我有一个数组,需要在Ios 从日期数组创建年份的节,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个数组,需要在UITableView的部分中显示 我目前在一个部分下按日期顺序显示所有对象,但我需要按年份对它们进行分区,我不确定如何进行 我的目标有点像 @interface MyEvent : NSObject @property NSDate *date; @property NSString *title; @property NSString *detail; @end 我的数组是这些对象按日期顺序排列的数组 我可以直接从这个数组中执行此操作,还是需要将数组拆分为二维数组
UITableView
的部分中显示
我目前在一个部分下按日期顺序显示所有对象,但我需要按年份对它们进行分区,我不确定如何进行
我的目标有点像
@interface MyEvent : NSObject
@property NSDate *date;
@property NSString *title;
@property NSString *detail;
@end
我的数组是这些对象按日期顺序排列的数组
我可以直接从这个数组中执行此操作,还是需要将数组拆分为二维数组
i、 e.NSArray的NSArray,其中第二个NSArray中的每个对象都在同一年。您可以从以下链接获得帮助: 使用from作为数据结构很容易做到这一点。基于块的初始值设定项提供了将数据组织到各个部分的几种方法之一:
NSArray *sortedEvents = ...; // events sorted by date
TLIndexPathDataModel *dataModel = [[TLIndexPathDataModel alloc] initWithItems:sortedEvents sectionNameBlock:^NSString *(id item) {
MyEvent *event = (MyEvent *)item;
NSString *year = ...; // calculate section name for the given item from date
return year;
} identifierBlock:nil];
然后,使用数据模型API,数据源方法变得非常简单:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.dataModel.numberOfSections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dataModel numberOfRowsInSection:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellId = ...;
UITableViewCell *cell = ...; // dequeue cell
MyEvent *event = [self.dataModel itemAtIndexPath:indexPath];
... // configure cell
return cell;
}