Ios UICollectionView插入项SatindExpaths:引发错误
我知道有各种各样的问题与此相关,但没有一个答案回答了这个问题 无论如何,我有一个UICollectionView,它应该永远滚动,但我似乎无法让Ios UICollectionView插入项SatindExpaths:引发错误,ios,uicollectionview,Ios,Uicollectionview,我知道有各种各样的问题与此相关,但没有一个答案回答了这个问题 无论如何,我有一个UICollectionView,它应该永远滚动,但我似乎无法让insertItemSatinExpaths:工作。它引发了一个错误: *** Assertion failure in -[UICollectionView _endItemAnimations], /SourceCache/UIKit/UIKit-2935.137/UICollectionView.m:3688 这是我的密码: #import "I
insertItemSatinExpaths:
工作。它引发了一个错误:
*** 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
#导入“ImageSlideServiceController.h”
#定义项目单元标识符@“项目单元”
@接口ImageSliderViewController()
@结束
@ImageSlideServiceController的实现
-(无效)viewDidLoad
{
[超级视图下载];
[自设置集合视图];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
}
#pragma标记-UICollectionView方法
-(无效)setupCollectionView{
self.items=[[NSMutableArray alloc]init];
加载间隙=5;
self.collectionView.delegate=self;
self.collectionView.dataSource=self;
[self.collectionView注册表类:[ImageSliderCell类]forCellWithReuseIdentifier:ITEM_CELL_IDENTIFIER];
UICollectionViewFlowLayout*flowLayout=[[UICollectionViewFlowLayout alloc]init];
[flowLayout设置滚动方向:UICollectionViewScrollDirection水平];
[flowLayout SetMinimuminemSpacing:0.0f];
[流程布局设置最小线间距:0.0f];
[self.collectionView SetPaginEnabled:是];
[self.collectionView setCollectionViewLayout:flowLayout];
[自备邮件];
}
-(NSInteger)collectionView中的节数:(UICollectionView*)collectionView{
返回1;
}
-(NSInteger)collectionView:(UICollectionView*)collectionView项目编号截面:(NSInteger)截面{
返回self.items.count;
}
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath{
ImageSliderCell*单元格=(ImageSliderCell*)[collectionView dequeueReusableCellWithReuseIdentifier:ITEM\u cell\u indexPath标识符:indexPath];
cell.imageTitle.text=[NSString stringWithFormat:@“%ld”,(长)indexPath.item];
NSLog(@“%d:%d”,indexath.item,self.items.count-1);
if(indexath.item>=self.items.count-1){
[自备邮件];
}
返回单元;
}
-(CGSize)collectionView:(UICollectionView*)collectionView布局:(UICollectionViewLayout*)collectionViewLayout大小FormiteIndeXPath:(NSIndexPath*)indexPath{
CGSize size=self.collectionView.frame.size;
size.height=size.height/2;
返回大小;
}
-(作废)装货港
{
NSLog(“加载新项目”);
NSUInteger loopTill=self.items.count+loadingGap;
NSMUTABLEARRY*indexPathsToLoad=[NSMUTABLEARRY new];
while(self.items.count
提前谢谢
更新:我更新了代码以使用reloadeitemsatindexpaths
和一些基于建议的其他改进。谢谢
[NSIndexPath indexPathForItem:self.items.count inSection:0]
应该是:
[NSIndexPath indexPathForItem:self.items.count - 1 inSection:0]
更新
您应该通过在循环中建立索引路径的
NSMutableArray
并在末尾插入它们(如您所做和注释所示),一次提交所有插入。或者,您可以将循环插入到performbatchUpdate
块中。谢谢您的回答
看起来我在另一个线程上尝试其中一个解决方案时出现了计算错误。以下是我的新解决方案:
-(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];
}
}
-(void)addNewItems
{
NSLog(“加载新项目”);
NSMUTABLEARRY*indexPathsToLoad=[NSMUTABLEARRY new];
对于(int i=0;i
谢谢大家! 用剩下的错误更新你的问题。@maddy-这就是它给我的全部。谢谢日志应该会提供更多关于断言错误的详细信息。@maddy-它没有,这就是它给我的全部信息。我希望我有更多的信息,因为我完全被难住了。如果在Xcode中调试时出现此错误,Xcode控制台肯定会显示有关断言原因的更多详细信息。更新了我的答案。您有两个问题,第二个问题是不能在批处理更新块之外多次调用
insertItemsAtIndexPaths
。