当前位置:  开发笔记 > IOS > 正文

对于tableview部分,按日期对NSArray组进行分组

如何解决《对于tableview部分,按日期对NSArray组进行分组》经验,为你挑选了1个好方法。

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

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

下面是阵列结果

DATA (

" {\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}",

" {\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}",

" {\n    date = \"2013-11-20 07:00:00 +0000\";\n    description = Breakfast;\n    location = Ballrooms;\n    name = Breakfast;\n}",

" {\n    date = \"2013-11-20 11:30:00 +0000\";\n    description = Lunch;\n    location = Ballrooms;\n    name = Lunch;\n}",

" {\n    date = \"2013-11-20 17:00:00 +0000\";\n    description = \"Cocktails - Ballroom Pre-Function Area\";\n    location = Ballroom;\n    name = Cocktails;\n}",

" {\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}",

" {\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}",

" {\n    date = \"2013-11-21 07:30:00 +0000\";\n    description = Breakfast;\n    location = \"Center Stage\";\n    name = Breakfast;\n}",

" {\n    date = \"2013-11-21 12:00:00 +0000\";\n    description = Lunch;\n    location = \"Center Stage\";\n    name = Lunch;\n}",

" {\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}",

" {\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}",

" {\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}",

" {\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}",

" {\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}",

" {\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}",

" {\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];

arturdev.. 5

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];

这是一个快速编写的代码,很抱歉可能出现错误:)



1> arturdev..:
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];

这是一个快速编写的代码,很抱歉可能出现错误:)

推荐阅读
wurtjq
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有