我是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中......可能出错了什么以及我应该更改什么?
mateusz
如果有人仍然对我的代码感兴趣:
当我的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;
}