2014-05-17 3 views
0

모든 종류의 질문이 있지만 관련 답변이 없습니다.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을 구축하고 그들을 삽입하여 한 번에 삽입을 모두 제출해야합니다

+0

질문을 나머지 오류와 함께 업데이트하십시오. – rmaddy

+0

@maddy - 그게 내게주는 전부 야. 감사! – Zoyt

+0

로그는 어설 션 오류에 대한 더 자세한 정보를 제공해야합니다. – rmaddy

답변

1
[NSIndexPath indexPathForItem:self.items.count inSection:0] 

는이어야한다 끝내기 (끝내고 주석 처리 한 것처럼). 또는 performBatchUpdates 블록의 루프 인세 트를 넣을 수도 있습니다.

+0

고마워,하지만 여전히 같은 오류가 발생합니다. – Zoyt

+0

내 대답이 업데이트되었습니다. 두 번째 문제는 배치 업데이트 블록 외부에서'insertItemsAtIndexPaths'를 여러 번 호출 할 수 없다는 것입니다. –

+0

여전히 작동하지 않지만 감사합니다! – Zoyt

0

모든 답변에 감사드립니다.

다른 스레드에서 솔루션 중 하나를 시도 할 때 계산 오류가있는 것처럼 보입니다. 작동하는 새로운 솔루션이 있습니다.

-(void)addNewItems 
{ 
    NSLog(@"Loading new items"); 
    NSMutableArray *indexPathsToLoad = [NSMutableArray new]; 
    for (int i = 0; i < LOADING_GAP; i++) { 
     [indexPathsToLoad addObject:[NSIndexPath indexPathForItem:self.items.count inSection:0]]; 
     [self.items addObject:@"Test"]; 
    } 
    if (self.items.count == LOADING_GAP) { 
     [self.collectionView reloadData]; 
    } else { 
     [self.collectionView insertItemsAtIndexPaths:indexPathsToLoad]; 
    } 
} 

고마워요!