gpt4 book ai didi

ios - UICollectionView insertItemsAtIndexPaths : raising error

转载 作者:行者123 更新时间:2023-11-29 02:57:39 25 4
gpt4 key购买 nike

我知道有各种各样与此相关的问题,但没有一个答案回答了这个问题。

无论如何,我有一个应该永远滚动的 UICollectionView,但我似乎无法让 insertItemsAtIndexPaths: 工作。它会引发错误:

*** Assertion failure in -[UICollectionView _endItemAnimations], /SourceCache/UIKit/UIKit-2935.137/UICollectionView.m:3688

这是我的代码:

#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

提前致谢!

更新:我根据建议更新了代码以使用 reloadItemsAtIndexPaths 和其他一些改进。谢谢!

最佳答案

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

应该是:

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

更新

并且您应该通过在循环中构建索引路径的 NSMutableArray 并在末尾插入它们(就像您已经完成并注释掉的那样)来一次提交所有插入。或者,您可以将循环插入 performBatchUpdates block 。

关于ios - UICollectionView insertItemsAtIndexPaths : raising error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23707186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com