我的 UICollectionView 中有多个按“datasetType”分组的部分。
我还为每个部分定制了一个 UICollectionViewCell。
如何确定方法UICollectionViewCell
中需要的自定义cellForItemAtIndexPath
项?
我的第一个虽然是基于[sectionInfo name]
:
id sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:indexPath.section];
if ([[sectionInfo name] isEqualToString:@"DatasetA"]) {
DatasetACell *datasetACell = [collectionView dequeueReusableCellWithReuseIdentifier:datasetACellIdentifer forIndexPath:indexPath];
DatasetA *datasetA = [self.fetchedResultsController objectAtIndexPath:indexPath];
}
但是我遇到了一个问题,它试图加载错误的数据集单元。
这是否可行,我需要在其他地方寻找错误?还是我做错了这部分?
编辑:
我所拥有的应该工作。问题是索引不对齐。
对于我的 fetchedResultsController,我将其创建为:
_fetchedResultsController = [Dataset MR_fetchAllSortedBy:NAME
ascending:TRUE
withPredicate:predicate
groupBy:@"datasetType"
delegate:self.fetchedResultsController.delegate
inContext:[NSManagedObjectContext MR_contextForCurrentThread]];
这会获取它们并按名称对它们进行排序。我在项目中使用 Magical Record,并使用它的 fetch 控制器,因为我无法让 Core Data fetch 控制器工作:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Dataset" inManagedObjectContext:[NSManagedObjectContext MR_contextForCurrentThread]];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:NAME ascending:TRUE selector:@selector(caseInsensitiveCompare:)];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error;
NSArray *fetchedArray = [[NSManagedObjectContext MR_contextForCurrentThread] executeFetchRequest:fetchRequest error:&error];
NSLog(@"fetchedArray: %@", fetchedArray);
NSFetchedResultsController *FRC = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[NSManagedObjectContext MR_contextForCurrentThread] sectionNameKeyPath:nil cacheName:nil];
FRC.delegate = self;
我在这段代码中执行的 fetchRequest 返回实体,一旦我将它通过 fetch 控制器,它总是不返回任何内容。我不认为它喜欢 MR 的上下文。/惊奇
当我尝试填充每个子类UICollectionViewCell
(DatasetACell、DataseetBCell 等)时,它可能会检索到错误的数据单元格。
所以在 indexPath (0,0) 处,在NSFetchedResultsController
[self.fetchedResultsController sections] objectAtIndex:indexPath]
将返回一个 datasetA 实体,而同一索引的集合视图
[collectionView dequeueReusableCellWithReuseIdentifier:datasetACell forIndexPath:indexPath]
返回一个数据集BCell。
NSFetchedResultsController
已排序[collectionView dequeueResuableCellwithReuseIdentifier:forIndexPath]
但未排序时存在问题。
完整代码:
-(NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"boundary.boundaryID == %@", self.dataseter.boundaryID];
_fetchedResultsController = [Dataset MR_fetchAllSortedBy:NAME
ascending:TRUE
withPredicate:predicate
groupBy:@"datasetType"
delegate:_fetchedResultsController.delegate
inContext:[NSManagedObjectContext MR_contextForCurrentThread]];
return _fetchedResultsController;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
id sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:indexPath.section];
NSLog(@"indexPath: %@", indexPath);
NSLog(@"[sectionInfo name]: %@", [sectionInfo name]);
if ([[sectionInfo name] isEqualToString:SAVED_ANALYSIS]) {
NSLog(@"section name is DATASET A");
DatasetCellA *datasetCellA = (DatasetCellA *)[collectionView dequeueReusableCellWithReuseIdentifier:datasetcellAIdentifier forIndexPath:indexPath];
DatasetA *datasetA = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"datasetA.id: %@", datasetA.id);
NSLog(@"datasetA: %@", datasetA);
// configure and return cell
}
else if ([[sectionInfo name] isEqualToString:EDITED_IMAGES]) {
NSLog(@"section name is DATASET B");
DatasetCellB *datasetCellB = (DatasetCellB *)[collectionView dequeueReusableCellWithReuseIdentifier:datasetCellBIdentifer forIndexPath:indexPath];
DatasetB *datasetB = [self.fetchedResultsController objectAtIndex:indexPath];
NSLog(@"editedImage.id: %@", datasetB.id);
NSLog(@"editedImage: %@", datasetB);
// configure and return cell
}
}
日志:
2013-04-04 10:59:38.697 [2380:14003] indexPath: <NSIndexPath 0x19b645c0> 2 indexes [0, 0]
2013-04-04 10:59:38.697 [2380:14003] [sectionInfo name]: Dataset B
2013-04-04 10:59:38.697 [2380:14003] section name is DATASET B
2013-04-04 10:59:38.702 [2380:14003] datasetB.id: 1581
2013-04-04 10:59:38.703 [2380:14003] datasetB: <DatasetA: 0x1c193ac0> (entity: DatasetA; id: 0x16141bd0 <x-coredata://9313C8D3-0AA8-4F3F-B32D-F1F7843D4FA1/DatasetA/p168> ; data: {
})
2013-04-04 10:59:38.703 [2380:14003] indexPath: <NSIndexPath 0x19b64560> 2 indexes [1, 0]
2013-04-04 10:59:38.703 [2380:14003] [sectionInfo name]: Dataset A
2013-04-04 10:59:38.704 [2380:14003] section name is DATSET A
2013-04-04 10:59:38.709 [2380:14003] datasetA.id: 75
2013-04-04 10:59:38.709 [2380:14003] datasetA: <DatasetB: 0x1c15a830> (entity: DatasetB; id: 0x16141b30 <x-coredata://9313C8D3-0AA8-4F3F-B32D-F1F7843D4FA1/DatasetB/p165> ; data: {
})
2013-04-04 10:59:38.724 [2380:14003] -[DatasetB mode]: unrecognized selector sent to instance 0x1c15a830
2013-04-04 10:59:38.725 [2380:14003] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DatasetB mode]: unrecognized selector sent to instance 0x1c15a830'
*** First throw call stack:
(0x1c19012 0x1a3ee7e 0x1ca44bd 0x1c08bbc 0x1c0894e 0x80028 0xe862da 0xe878f4 0xe89b91 0x9d32dd 0x1a526b0 0x56cfc0 0x56133c 0x561150 0x4df0bc 0x4e0227 0x4e08e2 0x1be1afe 0x1be1a3d 0x1bbf7c2 0x1bbef44 0x1bbee1b 0x28cc7e3 0x28cc668 0x982ffc 0x1ebd 0x1de5)
libc++abi.dylib: terminate called throwing an exception
对于第一个索引(0,0),代码认为它是DatasetB,但实际上是DatasetA。在第二个索引 (1,0) 中相反;代码认为它是一个DatasetA,但它是一个DatasetB。在这个集合中,有 1 个 DatasetB 和许多 DatasetA。
当 fetch 控制器对其进行排序时,它首先对 Dataset B 进行排序。集合视图需要一个 DatasetB,但 fetch 控制器正在为同一索引返回一个 DatasetA。