1

我有一个看起来像这样的数据模型:

Location <------->> Item

每个位置可以有许多项目。我想在UICollectionView. 我希望每个位置都是包含项目的部分。像这样:

-----------------------
I   Location 1        I
-----------------------
I Item 1   I  Item 2  I
I--------- I-----------
I   Location 2        I
-----------------------
        [...]

我想知道哪种方法最好实现这一目标?我以前从未使用过部分,所以我不确定这是否是正确的方法。如果这是要走的路,我如何将位置作为部分获取,然后在这些部分中显示项目?

我目前的非工作实施看起来像这样:

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

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
    return [sectionInfo numberOfObjects];
}

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Location" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
    NSArray *sortDescriptors = @[sortDescriptor];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // 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:self.managedObjectContext  sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

[...]

    return _fetchedResultsController;
}

有什么想法或建议吗?

4

1 回答 1

6

您必须获取Item对象,并使用sectionNameKeyPath:参数根据相关位置将项目分组为部分:

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

// The first sort descriptor must use the sectionNameKeyPath key:
NSSortDescriptor *sort1 = [[NSSortDescriptor alloc] initWithKey:@"location.name" ascending:NO];
// The second sort descriptor sorts the items within each section:
NSSortDescriptor *sort2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
NSArray *sortDescriptors = @[sort1, sort2];
[fetchRequest setSortDescriptors:sortDescriptors];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                      managedObjectContext:self.managedObjectContext
                        sectionNameKeyPath:@"location.name"
                                 cacheName:nil];
于 2013-10-20T09:12:21.360 回答