모든 종류의 질문이 있지만 관련 답변이 없습니다.UICollectionView insertItemsAtIndexPaths : 오류 발생
어쨌든 나는 영원히 스크롤해야하는 UICollectionView를 가지고 있지만, 작동하려면 insertItemsAtIndexPaths:
을 얻지 못하는 것 같습니다. 그것은 오류가 발생합니다 : 사전에
#import "ImageSliderViewController.h"
#define ITEM_CELL_IDENTIFIER @"ItemCell"
@interface ImageSliderViewController()
@end
@implementation ImageSliderViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupCollectionView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - UICollectionView methods
-(void)setupCollectionView {
self.items = [[NSMutableArray alloc] init];
loadingGap = 5;
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass:[ImageSliderCell class] forCellWithReuseIdentifier:ITEM_CELL_IDENTIFIER];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[flowLayout setMinimumInteritemSpacing:0.0f];
[flowLayout setMinimumLineSpacing:0.0f];
[self.collectionView setPagingEnabled:YES];
[self.collectionView setCollectionViewLayout:flowLayout];
[self loadMorePosts];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.items.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
ImageSliderCell *cell = (ImageSliderCell *)[collectionView dequeueReusableCellWithReuseIdentifier:ITEM_CELL_IDENTIFIER forIndexPath:indexPath];
cell.imageTitle.text = [NSString stringWithFormat:@"%ld",(long)indexPath.item];
NSLog(@"%d : %d",indexPath.item,self.items.count-1);
if (indexPath.item >= self.items.count-1) {
[self loadMorePosts];
}
return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize size = self.collectionView.frame.size;
size.height = size.height/2;
return size;
}
- (void)loadMorePosts
{
NSLog(@"Loading new items");
NSUInteger loopTill = self.items.count + loadingGap;
NSMutableArray *indexPathsToLoad = [NSMutableArray new];
while (self.items.count < loopTill) {
[indexPathsToLoad addObject:[NSIndexPath indexPathForItem:self.items.count inSection:0]];
NSLog(@"Added section %d",self.items.count);
AsyncImageView *img = [[DatabaseFetcher alloc] getPostWithID:[NSString stringWithFormat:@"%lu",(unsigned long)self.items.count]
inSection:@"features"
withFrame:CGRectMake(0, 0, self.collectionView.frame.size.width, self.collectionView.frame.size.height)];
[self.items addObject:@[img]];
};
[self.collectionView reloadItemsAtIndexPaths:indexPathsToLoad];
}
@end
감사 :
여기*** Assertion failure in -[UICollectionView _endItemAnimations], /SourceCache/UIKit/UIKit-2935.137/UICollectionView.m:3688
내 코드입니다!
업데이트 :reloadItemsAtIndexPaths
및 기타 개선 사항을 제안을 기반으로 사용하도록 코드를 업데이트했습니다. 감사!
[NSIndexPath indexPathForItem:self.items.count - 1 inSection:0]
갱신 그리고 당신은 당신의 루프에서 인덱스 경로의 NSMutableArray
을 구축하고 그들을 삽입하여 한 번에 삽입을 모두 제출해야합니다
질문을 나머지 오류와 함께 업데이트하십시오. – rmaddy
@maddy - 그게 내게주는 전부 야. 감사! – Zoyt
로그는 어설 션 오류에 대한 더 자세한 정보를 제공해야합니다. – rmaddy