使用NSFetchedResult控制器和每个月的节标题 [英] working with a NSFetchedResult controller and section headers for each month

查看:472
本文介绍了使用NSFetchedResult控制器和每个月的节标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

使用fetchedresultcontroller为每个月设置不同的部分


我试图将每个月的 UITableview 拆分成几个部分。我开始使用苹果的 DateSectionTitles 示例。首先,我在我的 coredata 中添加了一个属性sectionIdentifier。然后在我的 managedobject 子类中添加了此方法。

   - (NSString *)sectionIdentifier {

//根据需要创建和缓存段标识符。

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

if(!tmp){
/ *
节按月和年组织。创建段标识符作为表示数字的字符串(年* 1000)+月;这样,他们将按照时间顺序正确地排序,而不管月份的实际名称。
* /
NSCalendar * calendar = [NSCalendar currentCalendar];

NSDateComponents * components = [日历组件:(NSYearCalendarUnit | NSMonthCalendarUnit)fromDate:[self date]];
tmp = [NSString stringWithFormat:@%d,([components year] * 1000)+ [components month]];
[self setPrimitiveSectionIdentifier:tmp];
}
NSLog(@tmp:%@,tmp);
return tmp;
}

这是我在我的titleForHeaderSection



- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id< NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections] objectAtIndex:section];


/ *
节信息来自事件的sectionIdentifier,它是一个表示数字(年* 1000)+月的字符串。
要显示节标题,请将年份和月份组件转换为字符串表示形式。
* /
static NSArray * monthSymbols = nil;

if(!monthSymbols){
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setCalendar:[NSCalendar currentCalendar]];
monthSymbols = [formatter monthSymbols];
}

NSInteger numericSection = [[TheSection name] integerValue];

NSInteger year = numericSection / 1000;
NSInteger month = numericSection - (year * 1000);

NSString * titleString = [NSString stringWithFormat:@%@%d,[monthSymbols objectAtIndex:month-1],year];
NSLog(@titelstring is:%@,titleString);
return titleString;
}

对于我的行数InSection我这样做。

  id< NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; 

NSInteger count = [sectionInfo numberOfObjects];
return count;

这是我的NumberOfSectionsInTableview

  NSInteger count = [[self.fetchedResultsController sections] count]; 
return count;

有人可以帮忙吗?



提前!



发生了什么问题



打印我的日志NSLog(@tmp:%@,tmp);
并且我得到以下错误:

  ** CoreData **:error:(NSFetchedResultsController)段名键路径sectionIdentifier的nil值。对象将被放置在未命名的部分
2012-10-10 10:41:17.475 RacingGenk [15785:c07] ***由于未捕获异常NSRangeException终止应用程序,原因:' - [__ NSCFArray objectAtIndex:]:指数(-1(或可能更大))超出边界(12)'




这里是我设置我的NSFetchresultcontroller

   - (void)getKalender //附加一个NSFetchRequest UITableViewController 
{
NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:@Kalender];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@dateascending:YES]];

self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.genkDatabase.managedObjectContext
sectionNameKeyPath:@Kalender.sectionIdentifiercacheName:@Root] ;
self.fetchedResultsController.delegate = self;
self.fetchedResultsController = self.fetchedResultsController;


解决方案

我认为必须

  sectionNameKeyPath:@sectionIdentifier

而不是

  sectionNameKeyPath:@Kalender.sectionIdentifier
pre>

构建已提取结果控制器。


Possible Duplicate:
Setting a different section for each month with fetchedresultcontroller

I am trying to split my UITableview into sections for each month. I start working with the DateSectionTitles example of apple. First of all I added in my coredata a attribute 'sectionIdentifier'. Then in my managedobject subclass I added this method.

- (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 * 1000) + month; 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) fromDate:[self date]];
        tmp = [NSString stringWithFormat:@"%d", ([components year] * 1000) + [components month]];
          [self setPrimitiveSectionIdentifier:tmp];
    }
    NSLog(@"tmp: %@",tmp);
    return tmp;
}

This is what i do in my titleForHeaderSection

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections] objectAtIndex:section];


    /*
     Section information derives from an event's sectionIdentifier, which is a string representing the number (year * 1000) + month.
     To display the section title, convert the year and month components to a string representation.
     */
    static NSArray *monthSymbols = nil;

    if (!monthSymbols) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setCalendar:[NSCalendar currentCalendar]];
        monthSymbols = [formatter monthSymbols];
    }

    NSInteger numericSection = [[theSection name] integerValue];

    NSInteger year = numericSection / 1000;
    NSInteger month = numericSection - (year * 1000);

    NSString *titleString = [NSString stringWithFormat:@"%@ %d", [monthSymbols objectAtIndex:month-1], year];
    NSLog(@"titelstring is: %@",titleString);
    return titleString;
}

For my number of rowsInSection I do this.

id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];

        NSInteger count = [sectionInfo numberOfObjects];
        return count;

And this for my NumberOfSectionsInTableview

NSInteger count = [[self.fetchedResultsController sections] count];
        return count;

Can anybody help?

Thanks in advance!

What is going wrong

First off all it don't prints my log in NSLog(@"tmp: %@",tmp); And also I get the following error:

**CoreData**: error: (NSFetchedResultsController) A section returned nil value for section name key path 'sectionIdentifier'. Objects will be placed in unnamed section
2012-10-10 10:41:17.475 RacingGenk[15785:c07] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (-1 (or possibly larger)) beyond bounds (12)'

EDIT Here is where I set my NSFetchresultcontroller

- (void)getKalender // attaches an NSFetchRequest to this UITableViewController
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Kalender"];
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]];

    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                        managedObjectContext:self.genkDatabase.managedObjectContext
                                                                sectionNameKeyPath:@"Kalender.sectionIdentifier" cacheName:@"Root"];
    self.fetchedResultsController.delegate = self;
    self.fetchedResultsController = self.fetchedResultsController;

解决方案

I think it must be

sectionNameKeyPath:@"sectionIdentifier"

instead of

sectionNameKeyPath:@"Kalender.sectionIdentifier"

in the construction of the fetched results controller.

这篇关于使用NSFetchedResult控制器和每个月的节标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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