NSFetchedResultsController只返回一个节

时间:2014-02-22 22:07:36

标签: ios uitableview core-data nsfetchedresultscontroller sections

出于某种原因,我的方法调用-numberOfSectionsInTableView总是返回一个部分,无论数据模型是什么样的。

以下是我的TableVIewController.m中的一些代码片段。如果需要,请随时提出更多要求

self.sortDesciptor = [[NSSortDescriptor alloc] initWithKey:@"weaponData" ascending:YES];
self.sortDescriptors = [NSArray arrayWithObjects:self.sortDesciptor, nil];
self.sortSectionKey = @"weaponData";

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"number of sections:%lu",(unsigned long)[[_fetchedResultsController sections] count]);
    return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]    objectAtIndex:section];
    NSLog(@"%lu", (unsigned long)[sectionInfo numberOfObjects]);
    return [sectionInfo numberOfObjects];
}

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

- (NSFetchedResultsController *)fetchedResultsController {
    if (_fetchedResultsController != nil)
    {
        return _fetchedResultsController;
    }
    sTCAppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context =[appDelegate managedObjectContext];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"TargetData" inManagedObjectContext:context];

    [fetchRequest setEntity:entity];
    [fetchRequest setFetchBatchSize:0];

    if (self.sortMethod == 0){
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
        NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
        [fetchRequest setSortDescriptors:sortDescriptors];
    }
    else{
        [fetchRequest setSortDescriptors:self.sortDescriptors];
    }

    NSFetchedResultsController *aFetchedRequestsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:self.sortSectionKey cacheName:nil];
    aFetchedRequestsController.delegate = self;
    self.fetchedResultsController = aFetchedRequestsController;

    NSError *error = nil;

    if (![self.fetchedResultsController performFetch:&error])
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}

在我的代码的这个例子中,想想如果我的“weaponData”实体中有两个“weaponData”对象会发生什么。假设第一个名为Sniper Rifle,第二个名为Assault Rifle。这应该创建两个部分,其中一个名为Sniper Rifle,另一个名为Assault Rifle。实际发生的是我只获得1个部分,并根据首先输入对象上下文的对象命名。

有任何线索如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您传入的是排序描述符而不是keyPath。只需传入@“weaponData”作为sectionNameKeyPath