1
我从服务器获取对象列表,并将日期作为属性。 我收到的单车项目,我需要安排他们在一张桌子,但除以天(部分)。按日期对对象进行分组
我有点麻烦,因为我可以修复循环中的所有内容。
我所做的是,使用NSDateFormatteris创建一个数组的段数。但从逻辑上讲,我不知道如何在循环内创建所有内容。
NSMutableArray *singleSectionArray = [[NSMutableArray alloc] init];
NSMutableArray *sectionsArray = [[NSMutableArray alloc] init];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
int i = 0;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
for (PFObject *object in objects) {
[df setDateFormat:@"MMMM d EEEE"];
NSString *dateString = [[NSString alloc] initWithFormat:@"%@",[df stringFromDate:object.createdAt]];
NSArray *dateArray = [dateString componentsSeparatedByString:@" "];
BOOL sectionExist = [sectionsArray containsObject:[dateArray objectAtIndex:1]];
if (sectionExist == 0) {
[sectionsArray addObject:[dateArray objectAtIndex:1]];
[singleSectionArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
object.createdAt,@"date",
object.objectId,@"objectId",
nil]];
} else {
//???
}
}
...
[self.tableView reloadData];
我会有这样的结构
//Section
NSArray *singleSectionArray = [[NSArray alloc] initWithObjects:@"Object 1", @"Object 2", @"Object 3", nil];
NSDictionary * singleSectionDictionary = [NSDictionary dictionaryWithObject: singleSectionArray forKey:@"data"];
[dataArray singleSectionDictionary];
//Section
NSArray *singleSectionArray = [[NSArray alloc] initWithObjects:@"Object 4", @"Object 5", nil];
NSDictionary * singleSectionDictionary = [NSDictionary dictionaryWithObject: singleSectionArray forKey:@"data"];
[dataArray singleSectionDictionary];
感谢
感谢您的回复,但你能告诉我如何在数组中输入数据吗?我无法做到。非常感谢你 – Vins