按日期xcode对NSMutableArray进行分组

时间:2015-03-23 10:24:41

标签: objective-c nsmutablearray

我有NSMutableArray个项目,其中包含itemNameeventDate

我已按日期顺序对数组进行了排序,但我还希望按日期对项目进行分组,以便它们显示为表格中的部分。

@property (nonatomic) NSMutableArray *items;

...

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey: @"eventDate" ascending: TRUE];
return [self.items sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

提前谢谢

2 个答案:

答案 0 :(得分:1)

试试这个:

NSSortDescriptor *dateSD= [NSSortDescriptor sortDescriptorWithKey: @"eventDate" ascending: YES];

NSMutableArray *sortedByDate= [UnsortedArray sortedArrayUsingDescriptors: dateSD];
NSMutableArray *sortedArray = [NSMutableArray arrayWithCapacity:[sortedByDate count]];

while([sortedByDate count]) 
{
        id groupLead = [sortedByDate objectAtIndex:0];  
        NSPredicate *itemNamePredicate = [NSPredicate predicateWithFormat:@"name = %@",itemName]; // use itemName as string here

        NSArray *item= [sortedByDate filteredArrayUsingPredicate: itemNamePredicate];

        [sortedArray addObjectsFromArray: item];
        [sortedByDate removeObjectsInArray: item];
}

现在检查sortedArray

请根据您的代码更新变量和属性名称。希望这可以帮助。

答案 1 :(得分:0)

您需要确定表格视图的日期增量,即每个部分代表同一秒,小时,日e.t.c中的项目。

假设你决定当天可以这样做:

// We will need a date formatter for later
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];   
[formatter setDateFormat:@"dd-MM-yyyy"];

// 1) Get the oldest and newest items
NSDate *oldest = [(Item *)items.firstObject eventDate];
NSDate *newest = [(Item *)items.lastObject eventDate];

// 2) Get an array with the relevant dates in it...
NSTimeInterval timeDiffSecs = [newest timeIntervalSinceDate:oldest];
int days = (((timeDiffSecs / 60) / 60) / 24) + 1;
NSMutableArray *dates = [[NSMutableArray alloc] init];
for (int i = 0; i < days; i++) {
    [dates addObject:[NSDate dateWithTimeInterval:((24 * 60) *60) * i since date:oldest]];
}

然后使用此数据,您需要实现以下UITableView数据源方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // This is the date that you will group the items under
    NSDate *sectionDate = dates[indexPath.section];
    NSArray *filteredItems = [items filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"eventDate >= %@ AND eventDate < %@", sectionDate, sectionDate]];
    Item *item = filteredItems[indexPath.row];
    // Then fill out the cell using the item.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the amount of rows for each given section...
    NSDate *sectionDate = dates[indexPath.section];
    return [[items filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"eventDate >= %@ AND eventDate < %@", sectionDate, sectionDate]] count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // This is easy this is just the number of dates we originally found...
    return dates.count;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    // This is asking for the section with applies to the given title
    // 1) Get the date from the string
    NSDate *date = [formatter dateFromString:title];
    NSArray *filtered = [dates filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF >= %@ AND SELF < %@", date, date]];
    // The above array should only contain 1 date
    return [dates indexOfObject:filtered[0]];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    // This is asking for the title for the given section
    NSDate *date = dates[section];
    return [formatter stringFromDate:date];
}

注意:这些数据源方法是您必须实现的,以便通过某个属性组织表格视图中的对象,例如:日期。但是,用于过滤日期e.t.c的代码仅作为示例,我还没有对其进行测试,因此可能需要进行一些调整。