gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:59:07 24 4
gpt4 key购买 nike

我正在尝试将我的表格 View 拆分为每个月的部分。我开始使用 DateSectionTitles苹果的例子。首先,我在我的核心数据库中添加了一个属性“sectionIdentifier”。然后在我的 managedobject 子类中我添加了这个方法。

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

这就是我在 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;
}

对于我的 rowsInSection 数量,我这样做。
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) 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)'

最佳答案

通常为 nsmanagedobject 编码 transient 属性的一个常见错误是人们忘记在数据模型文件 (xcdatamodeld) 中也“启用” transient 属性。

关于iphone - 使用 fetchedresultcontroller 为每个月设置不同的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12815093/

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