iOS,组表视图,由一个核心数据实体分组

时间:2015-04-27 12:49:18

标签: ios objective-c

My core Data

我有(订单)表查看控制器,按"代码"排序实体 我想要做的是分组表视图,按分组 order.customer.name就像这样

[customer one]
*order.code1 - product1
*order.code2 - product1
[customer two]
...

我在viewWillAppear中知道的是:

- (void)viewWillAppear:(BOOL)animated{


    NSEntityDescription *orderDescription = [NSEntityDescription entityForName: @"Order"
                                                           inManagedObjectContext:self.context];
    NSFetchRequest *request = [NSFetchRequest new];
    [request setEntity:orderDescription];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"code" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];

    self.controller = [[NSFetchedResultsController alloc]
                       initWithFetchRequest:request
                       managedObjectContext:self.context
                       sectionNameKeyPath:nil
                       cacheName:nil];


    NSError *error;
    [self.controller performFetch:&error];      
}

这可能也很有用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    // Configure the cell...
    NSManagedObject *managedObject = [self.controller objectAtIndexPath:indexPath];
    cell.textLabel.text = ((Order *) managedObject).code;
    cell.detailTextLabel.text = ((Order *) managedObject).product.name;
    return cell;
}

我找到了一些用于组表视图的解决方案,但大多数解决方案都不适用于核心数据 感谢

2 个答案:

答案 0 :(得分:1)

这是你想要的吗?

- (void)setupFetch:(NSManagedObjectContext*)context {
    NSEntityDescription *orderDescription = [NSEntityDescription entityForName: @"Order"
                                                    inManagedObjectContext:context];
    NSFetchRequest *request = [NSFetchRequest new];
    [request setEntity:orderDescription];

    NSSortDescriptor *sortDescriptorSection = [[NSSortDescriptor alloc] initWithKey:@"customer.name" ascending:YES];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"code" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptorSection, sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];

    self.controller = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                          managedObjectContext:context
                                                            sectionNameKeyPath:@"customer.name"
                                                                     cacheName:nil];


    NSError *error;
    [self.controller performFetch:&error];
    [self.tableView reloadData];
}

这应该根据客户名称创建组,但根据代码排序。

编辑:澄清。您想使用sectionNameKeyPath来实现您想要的。请注意,您需要使用NSFetchedResultsController中的部分,如下所示:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [self.controller.sections objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.controller.sections count];
}

答案 1 :(得分:0)

我对会议室设备的情况类似。我的部分是房间,我的单元格是设备,首先按我的RoomName排序,然后按DeviceName

排序
- (NSFetchedResultsController *)fetchedResultsController
{
    if( !fetchedResultsController )
    {
        // Create the fetch request for the entity.
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        // Edit the entity name as appropriate.
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Device"
                                                  inManagedObjectContext:del.managedObjectContext];
        [fetchRequest setEntity:entity];

        // Edit the sort key as appropriate. This will sort the sections (setting this attribute as sectionNameKeyPath)
        NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"room.roomName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
        NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"deviceName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil];
        [sortDescriptor1 release];
        [sortDescriptor2 release];

        [fetchRequest setSortDescriptors:sortDescriptors];

        NSPredicate* predicate = ...; // selecting devices by filter
        [fetchRequest setPredicate:predicate];

        // Edit the section name key path and cache name if appropriate.
        // nil for section name key path means "no sections".
        NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                                                    managedObjectContext:managedObjectContext
                                                                                                      sectionNameKeyPath:@"room.roomName"
                                                                                                               cacheName:nil];

        aFetchedResultsController.delegate = self;
        self.fetchedResultsController = aFetchedResultsController;

        [aFetchedResultsController release];
        [fetchRequest release];
        [sortDescriptors release];
    }

    return fetchedResultsController;
}

使用如下:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSError *error = nil;
    if( ![[self fetchedResultsController] performFetch:&error] )
    {
        // do error handling
        abort();
    }

    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    int nor = [[fetchedResultsController sections] count];
    return nor;
}

- (NSString *)tableView:(UITableView *)tableView  titleForHeaderInSection:(NSInteger)section
{
    // I have a Translation class for internationalisation
    return [Translation forKey:[[[fetchedResultsController sections] objectAtIndex:section] name]];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger numberOfRows = 0;

    if ([[fetchedResultsController sections] count] > 0) {
        id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
        numberOfRows = [sectionInfo numberOfObjects];
    }

    return numberOfRows;
}

并在单元格中使用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Device *device = [fetchedResultsController objectAtIndexPath:indexPath];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:...];
    if( !cell )
    {
        cell = ...
        // and so on
    }

    return cell;
}