问题
I want my NSArray sampleData to receive actual data from parse.com database assuming like this:
self.sampleData = @[ @{ @"date": @"12/5/2014",
@"group": @[ @{ @"text": @"post1", @"location": @"x,y" },
@{ @"text": @"post2", @"location": @"x,y" },
@{ @"text": @"post3", @"location": @"x,y" },
@{ @"text": @"post4", @"location": @"x,y" },
@{ @"text": @"post5", @"location": @"x,y" }
]
},
@{ @"date": @"12/3/2014",
@"group": @[ @{ @"text": @"post6", @"location": @"x,y" },
@{ @"text": @"post7", @"location": @"x,y" },
@{ @"text": @"post8", @"location": @"x,y" },
@{ @"text": @"post9", @"location": @"x,y" },
@{ @"text": @"post10", @"location": @"x,y" }
]
}
];
As you can see, I want to group text and location by date, so that I can display them in a view with date as header and text/location as content. Here below is what I'm capable doing so far:
PFQuery *postQuery = [PFQuery queryWithClassName:kPAWParsePostsClassKey];
[postQuery whereKey:kPAWParseUserKey equalTo:[PFUser currentUser]];
postQuery.cachePolicy = kPFCachePolicyNetworkElseCache;
postQuery.limit = 20;
[postQuery findObjectsInBackgroundWithBlock:^(NSArray *myPosts, NSError *error)
{
if( !error )
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy"];
NSMutableArray *objectArray = [NSMutableArray new];
for (PFObject *object in myPosts) {
[objectArray addObject:@{@"createdAt": [formatter stringFromDate:object.createdAt], @"text": [object objectForKey:@"text"], @"location": [object objectForKey:@"location"]}];
}
self.sampleData = objectArray;
NSLog(@"My sampleData --> %@", self.sampleData);
}
}
];
The above code is obvious there's no grouping whatsoever, so really need help here.
回答1:
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;
}
来源:https://stackoverflow.com/questions/27317469/add-objects-to-nsmutable-array-with-grouping