gpt4 book ai didi

ios - 按 TableView 部分的日期对 NSArray 结果进行分组

转载 作者:行者123 更新时间:2023-11-28 18:33:46 26 4
gpt4 key购买 nike

我有以下 NSArray,我想用数组对包含相同日期的所有对象进行分组。我想对这些对象进行分组,然后将它们显示在 TableView 中,并将具有相同日期的对象分组在 TableView 中各自的部分中。在每个部分中,我希望按事件发生的时间从最早到最晚列出事件。

检索到的数据来自 Parse 数据库。 (不确定这是否重要)。

下面是数组结果

DATA (

"<Sessions:QtJKSToEu9:(null)> {\n date = \"2013-11-19 18:30:00 +0000\";\n description = \"Seating assignment on name badge.\";\n location = \"Center Stage\";\n name = \"Dinner and CARE Awards Celebration\";\n}",

"<Sessions:btZ5eRZffl:(null)> {\n date = \"2013-11-19 20:30:00 +0000\";\n description = \"Brief meeting\";\n location = \"Cedar Room\";\n name = \"Store Manager Advisory Board Members\";\n}",

"<Sessions:HY4VtgBncd:(null)> {\n date = \"2013-11-20 07:00:00 +0000\";\n description = Breakfast;\n location = Ballrooms;\n name = Breakfast;\n}",

"<Sessions:6p6t2uL5Jf:(null)> {\n date = \"2013-11-20 11:30:00 +0000\";\n description = Lunch;\n location = Ballrooms;\n name = Lunch;\n}",

"<Sessions:TpEHi5Glsk:(null)> {\n date = \"2013-11-20 17:00:00 +0000\";\n description = \"Cocktails - Ballroom Pre-Function Area\";\n location = Ballroom;\n name = Cocktails;\n}",

"<Sessions:e9yEi6vFoE:(null)> {\n date = \"2013-11-20 18:00:00 +0000\";\n description = \"Welcome from Tony Kenney followed by dinner\";\n location = Ballrooms;\n name = Welcome;\n speaker = \"Tony Kenney\";\n}",

"<Sessions:kTG4NemPX4:(null)> {\n date = \"2013-11-20 19:30:00 +0000\";\n description = \"We Are Speedway Awards Night\";\n location = \"Center Stage\";\n name = \"Awards Night\";\n}",

"<Sessions:L9I8BIVECe:(null)> {\n date = \"2013-11-21 07:30:00 +0000\";\n description = Breakfast;\n location = \"Center Stage\";\n name = Breakfast;\n}",

"<Sessions:WUGRHDpHyo:(null)> {\n date = \"2013-11-21 12:00:00 +0000\";\n description = Lunch;\n location = \"Center Stage\";\n name = Lunch;\n}",

"<Sessions:r61f5NZr66:(null)> {\n date = \"2013-11-21 09:15:00 +0000\";\n description = \"Marketing Breakouts \\U2013 Rotation\\no\\tStart times for rotations: 9:15, 9:50, 10:25, 11:00, 11:35\\no\\tPlease see your name tag for your Group #2 room assignment.\\no\\tThe speaker will direct you to your next session.\\n\";\n location = Various;\n name = \"Marketing Breakouts \";\n}",

"<Sessions:RXkIDNIYoQ:(null)> {\n date = \"2013-11-20 13:30:00 +0000\";\n description = \"o\\tPlease see your name tag for your Group #1 room assignment\";\n location = Various;\n name = \"Division Breakouts\";\n}",

"<Sessions:lLI2PxdCdx:(null)> {\n date = \"2013-11-21 13:30:00 +0000\";\n description = \"Closing Comments by Tony Kenny\";\n location = \"Center Stage\";\n name = \"Closing Comments\";\n speaker = \"Tony Kenny\";\n}",

"<Sessions:KWz7Xen5a6:(null)> {\n date = \"2013-11-20 13:00:00 +0000\";\n description = \"Special Guest Speaker Gary Heminger\";\n location = \"Center Stage\";\n name = \"Special Guest Speaker\";\n speaker = \"Gary Heminger\";\n}",

"<Sessions:AU1x3LlUrF:(null)> {\n date = \"2013-11-21 08:30:00 +0000\";\n description = \"Marketing Focus by Tom LeFevers\";\n location = \"Center Stage\";\n name = \"Marketing Focus\";\n speaker = \"Tom LeFevers\";\n}",

"<Sessions:36TIRrD0s5:(null)> {\n date = \"2013-11-20 08:00:00 +0000\";\n description = \"Operations Focus by Glenn Plumby\";\n location = \"Center Stage\";\n name = \"Operations Focus\";\n speaker = \"Glenn Plumby\";\n}",

"<Sessions:tTvppNFM7L:(null)> {\n date = \"2013-11-19 17:00:00 +0000\";\n description = \"Cocktails \\U2013 Center Stage pre-function area. NOTE: Group photographs scheduled for select field personnel\\nin the lobby bar. (Appointment time on name badge.)\";\n location = \"Center Stage pre-function area \";\n name = Cocktails;\n}"

)

如何将包含相同日期(例如“2013-11-19”)的对象全部归为一个部分?

更新 使用 Artur Answers + 轻微修改以满足我的需要

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
int section = 0;

dataSource = [NSMutableArray new];
[dataSource addObject:[NSMutableArray array]];

for (int i = 0; i < _events.count - 1; i++) {
EventInfo *firstEvent = _events[i];
EventInfo *secondEvent = _events[i+1];

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MMMM, dd"];

NSString *firstDateString = [df stringFromDate:firstEvent.details.date];
NSString *secondDateString = [df stringFromDate:secondEvent.details.date];


sectionArray = dataSource[section];

if (![sectionArray containsObject:firstEvent]) {
[sectionArray addObject:firstEvent];
}
if (![firstDateString compare:secondDateString] == NSOrderedSame) {
NSMutableArray *newSection = [NSMutableArray array];
[newSection addObject:secondEvent];
[dataSource addObject:newSection];

section++;
}
}
[dataSource.lastObject addObject:_events.lastObject];

最佳答案

NSMutableArray *dataSource = [NSMutableArray new];

// Sorting your array by date asc
array = [[array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
YourObject *firstObject = obj1;
YourObject *secondObject = obj2;

return [secondObject.date compare:firstObject.date] == NSOrderedAscending;
}] mutableCopy];


int section = 0;
[dataSource addObject:[NSMutableArray array]]; //Adding first section

for (int i = 0; i < array.count - 1; i++) {
YourObject *firstObject = array[i];
YourObject *secondObject = array[i+1];

NSDate *firstDate = [self dateWithoutTimePart:firstObject.date];
NSDate *secondDate = [self dateWithoutTimePart:secondObject.date];

NSMutableArray *sectionArray = dataSource[section];

if (![sectionArray containsObject:firstObject]) {
[sectionArray addObject:firstObject];
}
if (![firstDate compare:secondDate] == NSOrderedSame)
{
NSMutableArray *newSection = [NSMutableArray array];
[newSection addObject:secondObject];
[dataSource addObject:newSection];

section++;
}
}

// Adding last object to last section
[dataSource.lastObject addObject:array.lastObject];

这是一个快速编写的代码,对于可能出现的错误深表歉意:)

关于ios - 按 TableView 部分的日期对 NSArray 结果进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23202091/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com