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