我希望我的 NSArray SampleData 从 parse.com 数据库接收实际数据,假设如下:
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" }
]
}
];
如您所见,我想按日期对文本和位置进行分组,以便我可以在视图中显示它们,以日期作为标题,以文本/位置作为内容。
以下是我到目前为止能够做的事情:
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);
}
}
];
上面的代码显然没有任何分组,所以这里确实需要帮助。