我希望我的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);
}
}
];
上面的代码很明显,没有任何分组,所以真的需要帮助。
答案 0 :(得分:1)
好的,所以你有一系列的项目,并且你想根据特定的密钥将它们分组。
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;
此代码不会生成完全您要求的内容。它会生成如下所示的内容:
sampleData = @[ @{ @"date": @"12/5/2014",
@"group": @@[ PFObject*,
PFObject*,
PFObject*,
PFObject*,
PFObject*,
]
},
@{ @"date": @"12/3/2014",
@"group": @[ PFObject*,
PFObject*,
PFObject*,
PFObject*,
PFObject
]
}
];
您无法将PFObject*
数组中的myPosts
转换为@{ @"text": @"post5", @"location": @"x,y" }
,因为您将无法访问其他信息。以下是使用此sampleData
数组的方法。
- (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;
}