如何在NSDate中使用NSPredicate按天分组 - 需要创建多个UITableView节 [英] How to Group by day in NSDate with NSPredicate - needed to create multiple UITableView sections

查看:253
本文介绍了如何在NSDate中使用NSPredicate按天分组 - 需要创建多个UITableView节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


核心数据按日期排序使用FetchedResultsController分成小节

有一种方法按照日期从CoreData实体分组对象,并使用它们在UITableView控件上创建多个节?

Is there a way to group objects from a CoreData entity by date and use them to create multiple sections on a UITableView control?

基本上,我需要一个等同于以下伪代码的东西, SQL,其中my_group_criteria()按天分组所有测试:

Essentially, I need something equivalent to the following pseudo-SQL, where my_group_criteria() groups all tests by day:

SELECT * FROM tests GROUP BY my_group_criteria(date)

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

As an example, the query would group the following rows into 3 separate sections:

[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不是用于分组实体,因此这可能不是要走的路。

As already answered in 2, NSPredicate is not for grouping entities so this may not be the way to go.

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

Given this, how do I go about creating multiple sections in a UITableView based on some criteria similar to what SQL GROUP BY would do?

我想避免直接访问SQL-lite数据库。

I would like to avoid accessing the SQL-lite database directly if at all possible.

相关问题1:
NSPredicate:过滤对象NSdate属性的日期

相关问题2:

Related question 2: NSPredicate something equivalent of SQL's GROUP BY

推荐答案

You可以根据需要从Apple Developer Library修改 DateSectionTitles 示例项目。

You can modify the DateSectionTitles sample project from the Apple Developer Library according to you needs.

首先,您必须修改瞬态 sectionIdentifier 属性的访问器函数,以创建基于年份的节标识符+ month + day(而不是year + month):

First, you have to modify the accessor function for the transient sectionIdentifier property to create a section identifier based on year+month+day (instead of year+month only):

- (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方法必须改变,但是想法是一样的:从段标识符中提取年,月和日,并从中创建一个标题标题字符串:

Second, the titleForHeaderInSection delegate method must be changed, but the idea is the same: extract year, month and day from the section identifer, and create a header title string from that:

- (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;
}

这篇关于如何在NSDate中使用NSPredicate按天分组 - 需要创建多个UITableView节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆