Add objects to NSMutable array with grouping

拜拜、爱过 提交于 2019-12-02 09:14:27

Okay, so you have an array of items, and you want to group them into sections based on a particular key.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy"];

// Sparse dictionary, containing keys for "days with posts"
NSMutableDictionary *daysWithPosts = [NSMutableDictionary dictionary];

[myPosts enumerateObjectsUsingBlock:^(PFObject *object, NSUInteger idx, BOOL *stop) {

    NSString *dateString = [formatter stringFromDate:[object createdAt]];

    // Check to see if we have a day already.
    NSMutableArray *posts = [daysWithPosts objectForKey: dateString];

    // If not, create it
    if (posts == nil || (id)posts == [NSNull null])
    {
        posts = [NSMutableArray arrayWithCapacity:1];
        [daysWithPosts setObject:posts forKey: dateString];
    }

    // add post to day
    [posts addObject:obj];
}];

// Sort Dictionary Keys by Date
NSArray *unsortedSectionTitles = [daysWithPosts allKeys];
NSArray *sortedSectionTitles = [unsortedSectionTitles sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    NSDate *date1 = [formatter dateFromString:obj1];
    NSDate *date2 = [formatter dateFromString:obj2];
    return [date2 compare:date1];
}];

NSMutableArray *sortedData = [NSMutableArray arrayWithCapacity:sortedSectionTitles.count];

// Put Data into correct format:
[sortedSectionTitles enumerateObjectsUsingBlock:^(NSString *dateString, NSUInteger idx, BOOL *stop) {
    NSArray *group = daysWithPosts[dateString];
    NSDictionary *dictionary = @{ @"date":dateString,
                                  @"group":group };
    [sortedData addObject:dictionary];
}];

self.sampleData = sortedData;

This code will not generate exactly what you asked for. It will generate something that looks like this:

sampleData = @[ @{ @"date": @"12/5/2014",
                    @"group": @@[ PFObject*,
                                 PFObject*,
                                 PFObject*,
                                 PFObject*,
                                 PFObject*,
                               ]
                    },
                 @{ @"date": @"12/3/2014",
                    @"group": @[ PFObject*,
                                 PFObject*,
                                 PFObject*,
                                 PFObject*,
                                 PFObject
                               ]
                    }
              ];

There's no need to convert your PFObject* in the myPosts array into @{ @"text": @"post5", @"location": @"x,y" } since you'll lose access to other pieces of information. Here is how you would use this sampleData array.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; {
    return self.sampleData.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; {
    return self.sampleData[section][@"date"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; {
    return self.sampleData[section][@"group"].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; {

    PFObject *post = self.sampleData[indexPath.section][@"group"][indexPath.row];
    UITableViewCell *cell = // dequeue A reusable tableviewcell here

    // configure the cell here

    return cell;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!