0

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

-(无效)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;

}

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

4

0 回答 0