我正在使用UICollectionView創建iPhone應用程序,並且正在嘗試創建類似iPhone彈簧板的文件夾。我有它的工作,但我遇到了關於使用核心數據的單元格的加法和減法這個錯誤。我希望有人能解釋我如何避免這個錯誤?先謝謝你。UICollectionView項目數量無效,數組添加和刪除元素
*終止應用程序由於未捕獲的異常「NSInternalInconsistencyException」,原因:「無效更新:在部分0項更新後包含在現有部分中的項目數的無效數(4)必須等於在更新之前包含在該部分中的項目數量(3),加上或減去從該部分插入或刪除的項目數量(0插入,0刪除)以及正或負移入或移出該部分的項目數量(0移入,0移出)。'
------ mainViewController
//**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;
}
你知道是什麼導致了問題,爲什麼這是解決方案?如果是這樣會改善這個問題/答案在這裏添加一些細節。 –
例如,此處顯示的保存可能只是簡單地觸發視圖的完整重建,儘管這會導致錯誤消失,但可能不會產生最佳性能,這可能會隨着應用程序的開發或您在設備上進行測試而出現問題。 –