我正在开发一个带有 UICollectionView 的 iPhone 应用程序,并且正在尝试创建像 iPhone 弹簧板这样的文件夹。我让它工作,但在使用核心数据添加和减去单元格时遇到了这个错误。我希望有人能解释我如何避免这个错误?先感谢您。
*由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:第0节中的项目数无效。更新后现有节中包含的项目数(4)必须等于其中包含的项目数更新前的那个部分 (3),加上或减去从该部分插入或删除的项目数(0 插入,0 删除),加上或减去移入或移出该部分的项目数(0 移入,0搬出)。'
------主视图控制器
//**Document One**
//get first doc and create it as a sub doc
Document * docToCopy = [[self birds] objectAtIndex:longPressFolderLayout.pinchedCellPath.row];
SubDocument *doc = [[SubDocument alloc] initInsertingIntoManagedObjectContext:managedObjectContext];
[doc setTitle:docToCopy.title];
[doc setThumbnail:docToCopy.thumbnail];
[doc setIsFolder:[NSNumber numberWithInt:0]];
[doc setDate:docToCopy.date];
//**Document Two**
//get second doc and create it as a sub doc
Document * docToCopy2 = [[self birds] objectAtIndex:cellAttributes.indexPath.row];
SubDocument *doc2 = [[SubDocument alloc] initInsertingIntoManagedObjectContext:managedObjectContext];
[doc2 setTitle:docToCopy2.title];
[doc setThumbnail:docToCopy2.thumbnail];
[doc2 setIsFolder:[NSNumber numberWithInt:0]];
[doc2 setDate:docToCopy2.date];
[self add:@"folder" image:[UIImage imageNamed:@"folderDefaultImage"] date:[NSDate date] folder:[NSNumber numberWithInt:1]];
//**Folder**
//create a folder and append both sub docs and then remove the original docs
Document * origFolder = [[self birds] objectAtIndex:([[self birds]count]-1)];
[origFolder appendDoc:doc];
[origFolder appendDoc:doc2];
[origFolder setIsFolder:[NSNumber numberWithInt:1]];
currentOpenFolder = origFolder;
[self removePageAtIndex:longPressFolderLayout.pinchedCellPath.row];
[self removePageAtIndex:cellAttributes.indexPath.row];
------DocumentNSManagedObject
- (void)appendDoc:(SubDocument *)doc
{
[[self mutableSetValueForKey:@"subDocument"] addObject:doc];
[(NSMutableArray *)[self subDocumentsOrdered] addObject:doc];
NSError *error = nil;
[[self managedObjectContext] save:&error];
}
- (void)deleteDoc:(SubDocument *)doc
{
[[self mutableSetValueForKey:@"subDocument"] removeObject:doc];
[(NSMutableArray *)[self subDocumentsOrdered]removeObject:doc];
NSError *error = nil;
[[self managedObjectContext] save:&error];
}
- (NSArray *)subDocumentsOrdered
{
if (!subDocumentsOrdered)
{
subDocumentsOrdered = [[[[self subDocument] allObjects] sortedArrayWithOptions:NSSortStable|NSSortConcurrent usingComparator:(NSComparator) ^(SubDocument *stroke1, SubDocument *stroke2)
{
return [[stroke1 date] compare:[stroke2 date]];
}
] mutableCopy];
}
return subDocumentsOrdered;
}