我是objective-c编程的新手。我正在尝试制作我的第一个应用程序,但我无法解决一个问题。
我在我的应用程序中使用 UICollectionView 和 NSFetchedResultController。该应用程序从 Web 下载数据并使用 NSOperationQueue 异步执行此操作。当我在模拟器中测试我的应用程序时一切正常,但是当我在 iPad 3 上测试它时,我经常遇到这个错误。
* -[UICollectionView _endItemAnimations] 中的断言失败,/SourceCache/UIKit/UIKit-2380.17/UICollectionView.m:2801
*由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的项目数无效。更新后现有节中包含的项目数 (28) 必须等于其中包含的项目数更新前的那个部分 (26),加上或减去从该部分插入或删除的项目数(1 插入,0 删除),加上或减去移入或移出该部分的项目数(0 移入,0搬出)。'
我不知道为什么,因为我将 maxConcurrentOperationCount 设置为 1,并且我认为在这种情况下,所有操作都会导致按顺序将项目插入 UICollectionView……可能有什么问题,我应该改变什么?
马图斯
如果有人仍然感兴趣,那是我的代码:
当我的 NSOperation 完成时,我正在调用此方法:
- (void)collectionViewUpdaterDidFinish:(CollectionViewUpdater *)updater {
[Artwork artworkWithBehanceInfo:self.projectsData
andImageData:updater.imageData
atIndex:updater.projectIndex
forIndexInDataBase:updater.indexPathInCollectionView.item withDataHelper:self.dataHelper];
NSIndexPath *indexPath = updater.indexPathInCollectionView;
[self.dataHelper fetchData];
[self.myCollectionView insertItemsAtIndexPaths:@[indexPath]];
[self.pendingOperations.downloadsInProgress removeObjectForKey:indexPath];
if ([self.dataHelper.fetchedResultsController.fetchedObjects count]==self.numberOfLastItemOnPage){
[self.refresh endRefreshing];
[self stopAnimation];
self.progressView.hidden = YES;
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ResultCell";
ResultCellView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
Artwork *artwork = [self.dataHelper.fetchedResultsController objectAtIndexPath:indexPath];
cell.title.text = [artwork.title stringByAppendingString:@"\n "];
cell.owner.text = [NSString stringWithFormat:@"%@",artwork.owner];
cell.fields.text = artwork.fields;
cell.numberOfViews.text = [NSString stringWithFormat:@"%@",artwork.views];
cell.numberOfLikes.text = [NSString stringWithFormat:@"%@",artwork.likes];
cell.numberOfComments.text = [NSString stringWithFormat:@"%@",artwork.comments];
cell.publishedDate.text = artwork.publishedDate;
if ([artwork.hasThumbnail boolValue]) {
dispatch_queue_t downloadQueue = dispatch_queue_create("imagecache", NULL);
dispatch_async(downloadQueue, ^{
UIImage *productImage = [UIImage imageWithData:artwork.thumbnail];
CGSize imageSize = productImage.size;
UIGraphicsBeginImageContext(imageSize);
[productImage drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
productImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(), ^ {
cell.thumbnailImage.image = productImage;
cell.thumbnailImage.hidden = NO;
});
});
}
else {
NSLog(@"Has to download thumbnail");
cell.thumbnailImage.hidden = YES;
[self startOperationsForPhotoRecord:artwork atIndexPath:indexPath];
}
if ([self.dataHelper.fetchedResultsController.fetchedObjects count]==self.numberOfLastItemOnPage) {
self.cellRefreshed = YES;
}
// it must be redifined for collections in behance!
if (indexPath.item >=(self.numberOfLastItemOnPage-10) &&self.cellRefreshed &&([self.projectsData count]%12==0)) {
self.numberOfFirstItemOnPage = self.numberOfLastItemOnPage+1;
self.numberOfLastItemOnPage +=12;
[self downloadMoreProjects];
self.cellRefreshed = NO;
}
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
cell.selectedBackgroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
return cell;
}