我知道我不是第一个问这个问题的人,但我真的很难过..
基本上我有一个带有两个按钮的屏幕。每个按钮根据日期将数据加载到下面的表格视图中。在第一个 tableview 的第一次加载(默认选择左侧按钮)时,一切都显示正常。如果我单击右侧按钮,我得到一个空白的 tableview,我得到错误
在索引 x 处获取的对象有一个乱序的部分名称“xxxxxx”。对象必须按部分名称排序。
切换回左表视图,数据消失了。两个表视图都是空的。
每个 tableview 有 2 个部分,具体取决于项目的开始时间。如果我消除这些部分,则数据显示正常。不幸的是我需要它们..数据被分为两个部分,如下所示:
@interface NSString(agendaSessionKeyPath)
@property (nonatomic, readonly) NSString *sessionSection;
@end
@implementation NSString(agendaSessionKeyPath)
- (NSString *)sessionSection
{
int timeValue = [[self stringByReplacingOccurrencesOfString:@":" withString:@""] intValue]; //turns 11:00 to 1100
if (timeValue < 1200)
return @"Morning";
else
return @"Afternoon";
}
获取请求
- (void)viewDidLoad
{
//other viewDidLoad stuff
[self fetchSessions];
}
根据日期对左右按钮中的数据进行排序的方法:
- (void)fetchSessions
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate* date = nil;
if (selected == 0) //left button is selected
{
date = [dateFormatter dateFromString:@"2012-09-26"];
}
else if (selected == 1) //right button is selected
{
date = [dateFormatter dateFromString:@"2012-09-27"];
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date == %@", date];
[self.fetchedResultsController.fetchRequest setPredicate:predicate];
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
}
获取结果控制器
- (NSFetchedResultsController *)fetchedResultsController {
self.managedObjectContext = [[MATCDatabaseController sharedDatabaseController] managedObjectContext];
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Session"];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"title" ascending:YES];
NSSortDescriptor *timeSort = [NSSortDescriptor sortDescriptorWithKey:@"timeValue" ascending:YES];
[fetchRequest setSortDescriptors:@[timeSort, sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"startTime.sessionSection"
cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
[self.fetchedResultsController setDelegate:self];
return _fetchedResultsController;
}
任何帮助表示赞赏!