And*_*ers 1 cocoa-touch core-data objective-c ios uicollectionview
我有一个看起来像这样的数据模型:
Location <------->> Item
Run Code Online (Sandbox Code Playgroud)
每个位置可以有很多项目.我想在a中显示这些位置和项目UICollectionView
.我希望每个位置都是包含项目的部分.像这样:
-----------------------
I Location 1 I
-----------------------
I Item 1 I Item 2 I
I--------- I-----------
I Location 2 I
-----------------------
[...]
Run Code Online (Sandbox Code Playgroud)
我想知道哪种方式最好达到这个目的?我之前从未使用过部分,所以我不确定这是不是正确的方法.如果是这样的话,我如何将位置作为部分,然后在这些部分中显示项目?
我目前的非工作实现如下:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
任何想法或建议?
您必须获取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];
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1686 次 |
最近记录: |