不确定为什么NSFetchedResultsController没有显示部分

时间:2013-07-26 21:16:04

标签: iphone ios objective-c core-data nsfetchedresultscontroller

我之前有一个TableView,可以从viewDidLoad中名为holdViewsArray的数组中获取其2个标题视图(和分隔符视图),如下所示:

- (void)viewDidLoad {

// ---Start Core Data With NSFetchedResultsController---
[super viewDidLoad];
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]){
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    exit(-1);
}
// ---End Core Data w/ NSFetchedResultsController---

[self.tableView setDelegate:self];
[self setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
holdViewsArray = [[NSMutableArray alloc]init];

UIView *seperatorView;
UIView *seperatorView2;

NSString *sectionTitle = @"Tasks To Complete";
NSString *section2Title = @"Completed Tasks";
UILabel *label = [[UILabel alloc]init];
UILabel *label2 = [[UILabel alloc]init];

label.frame = CGRectMake(10.0, 5.0, 320.0, 50.0);
label.text = sectionTitle;

label2.frame = CGRectMake(10.0, 0.0, 320.0, 40.0);
label2.text = section2Title;

headerView = [[UIView alloc]initWithFrame:label.frame];
headerView2 = [[UIView alloc]initWithFrame:label2.frame];

CGRect sepFrame = CGRectMake(0, headerView.frame.size.height-2, 320, 1);
CGRect sep2Frame =CGRectMake(0, headerView2.frame.size.height-2, 320, 1);

seperatorView = [[UIView alloc] initWithFrame:sepFrame];
seperatorView2 = [[UIView alloc]initWithFrame:sep2Frame];

[headerView addSubview:seperatorView];
[headerView2 addSubview:seperatorView2];
[headerView addSubview:label];
[headerView addSubview:button];
[headerView2 addSubview:label2];
[holdViewsArray addObject:headerView];
[holdViewsArray addObject:headerView2];

}

在实现NSFetchedResultsController之后,我决定使用NSFetchedResultsController's属性和方法来创建标题视图并将表视图拆分为2个部分。我试过这样做,但它没有在我的部分给我任何标题,它看起来很糟糕。我如何保持我现在拥有的标题的相同外观,但使用NSFetchedResultsController将表视图拆分为2个部分(完成和不完整的任务)?这是我试过的:

任务核心数据属性(可选属性):

@interface Tasks : NSManagedObject
@property (nonatomic, retain) NSString *sectionString;
@end

@implementation Tasks
@dynamic sectionString;
@end

在App Delegate

中的不同部分添加两项任务
NSManagedObjectContext *context = self.managedObjectContext;
    NSManagedObject *startingTask = [NSEntityDescription insertNewObjectForEntityForName:@"Tasks" inManagedObjectContext:context];
    [startingTask setValue:@"Eat Dinner" forKey:@"taskName"];
    [startingTask setValue:[NSNumber numberWithDouble:400] forKey:@"timeInterval"];
    [startingTask setValue:@"Tasks To Complete" forKey:@"sectionString"];

     NSManagedObject *finishedTask = [NSEntityDescription insertNewObjectForEntityForName:@"Tasks" inManagedObjectContext:context];
    [finishedTask setValue:@"Do Laundry" forKey:@"taskName"];
    [finishedTask setValue:[NSNumber numberWithDouble:400] forKey:@"timeInterval"];
    [finishedTask setValue:@"Completed Tasks" forKey:@"sectionString"];

    NSError *error;
    if (![context save:&error]) {
        NSLog(@"couldn't save: %@", [error localizedDescription]);
    } 

分为两个部分

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"Tasks" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

 NSSortDescriptor *isCompleted = [[NSSortDescriptor alloc]initWithKey:@"sectionString" ascending:NO];
    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                              initWithKey:@"dateCreated" ascending:NO];
    [fetchRequest setSortDescriptors:@[isCompleted, sort]];
    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController =
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                        managedObjectContext:managedObjectContext sectionNameKeyPath:@"sectionString"
                                                   cacheName:@"Root"];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;

}

我做错了什么导致它没有分成2个部分,标题为“要完成的任务”和“已完成的任务”?

0 个答案:

没有答案