ios - 如何使用 NSPredicate 在 NSDate 中按天分组 - 需要创建多个 UITableView 部分

标签 ios sql uitableview core-data

另见

Core Data Sort By Date With FetchedResultsController into sections

有没有办法按日期对 CoreData 实体中的对象进行分组,并使用它们在 UITableView 控件上创建多个部分?

基本上,我需要与以下伪 SQL 等效的东西,其中 my_group_criteria() 按天对所有测试进行分组:

SELECT * FROM tests GROUP BY my_group_criteria(date)

例如,查询会将以下行分为 3 个单独的部分:

[test1 | 2013-03-18 15.30.22]
[test2 | 2013-03-18 14.30.22]
[test3 | 2013-03-18 13.30.22]

[test4 | 2013-03-17 18.30.22]
[test5 | 2013-03-17 19.30.22]

[test6 | 2013-03-15 20.30.22]

已在 2 中回答,NSPredicate 不适用于对实体进行分组,因此这可能不是可行的方法。

鉴于此,我如何根据类似于 SQL GROUP BY 的一些标准在 UITableView 中创建多个部分?

如果可能的话,我想避免直接访问 SQL-lite 数据库。

相关问题一: NSPredicate: filtering objects by day of NSDate property

相关问题2: NSPredicate something equivalent of SQL's GROUP BY

最佳答案

您可以修改 DateSectionTitles根据您的需要从 Apple Developer Library 中获取示例项目。

首先,您必须修改 transient sectionIdentifier 属性的访问器函数,以创建基于年+月+日(而不是仅年+月)的部分标识符:

- (NSString *)sectionIdentifier {

    // Create and cache the section identifier on demand.

    [self willAccessValueForKey:@"sectionIdentifier"];
    NSString *tmp = [self primitiveSectionIdentifier];
    [self didAccessValueForKey:@"sectionIdentifier"];

    if (!tmp) {
        /*
         Sections are organized by month and year. Create the section identifier
         as a string representing the number (year * 10000 + month * 100 + day);
         this way they will be correctly ordered chronologically regardless of
         the actual name of the month.
         */
        NSCalendar *calendar = [NSCalendar currentCalendar];

        NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
                               fromDate:[self timeStamp]];
        tmp = [NSString stringWithFormat:@"%d", [components year] * 10000 + [components month] * 100  + [components day]];
        [self setPrimitiveSectionIdentifier:tmp];
    }
    return tmp;
}

其次,必须更改 titleForHeaderInSection 委托(delegate)方法,但思路是一样的:从节标识符中提取年、月和日,并从中创建标题标题字符串:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    id <NSFetchedResultsSectionInfo> theSection = [[fetchedResultsController sections] objectAtIndex:section];

    NSInteger numericSection = [[theSection name] integerValue];
    NSInteger year = numericSection / 10000;
    NSInteger month = (numericSection / 100) % 100;
    NSInteger day = numericSection % 100;

    // Create header title YYYY-MM-DD (as an example):
    NSString *titleString = [NSString stringWithFormat:@"%d-%d-%d", year, month, day];
    return titleString;
}

关于ios - 如何使用 NSPredicate 在 NSDate 中按天分组 - 需要创建多个 UITableView 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14942912/

相关文章:

ios - 如何使用 XIB 编辑 UITableView 上的单元格,就像 Storyboard 一样

ios - 将相同类型的 SKAction 添加到 sprite 会覆盖之前的 Action 吗?

iOS Kiwi/Nocilla 测试不调用 block

mysql - 按列自动排序(非主键)

sql - 在 Management Studio 中创建 CHANGE 脚本?

ios - FSPagerview - 是否可以在重复单元格内使用它?

ios - SWIFT TableView 错误

ios - xCode 在运行应用程序时失去与设备的连接

objective-c - 在 Objective C (iOS) 中拖放控件

sql - SQL中的递归选择