Objective c UICollection视图断言失败

Objective c UICollection视图断言失败,objective-c,uicollectionview,assertions,Objective C,Uicollectionview,Assertions,-(void)加载下一页失败 -[UICollectionView\u EndItemAnimationWithInvalidateContext:TentiveyForreOrdering:animator:]中的断言失败 无法将行插入节0。或者去一个新的部门。 每次调用api时,我都会尝试插入新的单元格。 json每页返回5个元素,因此pagecount @接口feedCollection() @属性(弱、非原子)IBUICollectionView*collectionView; @属性(

-(void)加载下一页
失败

-[UICollectionView\u EndItemAnimationWithInvalidateContext:TentiveyForreOrdering:animator:]中的断言失败

无法将行插入节0。或者去一个新的部门。
每次调用api时,我都会尝试插入新的单元格。 json每页返回5个元素,因此pagecount

@接口feedCollection()
@属性(弱、非原子)IBUICollectionView*collectionView;
@属性(弱、非原子)IBUICollectionViewFlowLayout*flowLayout;
@属性(非原子,赋值)仅布尔图像;
@属性(非原子,强)NSMutableArray*提要;
-(iAction)addFeed:(id)发送方;
-(iAction)changeLayoutDirection:(id)发送方;
@结束
静态常量CGFloat kNewPageLoadScrollPercentageThreshold=0.66;
@feedCollection的实现{
屏幕宽度;
整数页码;
int-sectionNum;
bool页面加载;
}
-(无效)viewDidLoad{
[超级视图下载];
页码=0;
sectionNum=1;
CGRect screenRect=[[UIScreen mainScreen]边界];
screenWidth=screenRect.size.width;
self.collectionView.delegate=self;
self.collectionView.dataSource=self;
//加载视图后,通常从nib执行任何其他设置。
self.feeds=[[NSMutableArray alloc]init];
}
-(无效)视图显示:(BOOL)动画{
[超级视图显示:动画];
[自取FeedAPI];
}
静态布尔值应加载下一页(UICollectionView*collectionView)
{
CGFloat yOffset=collectionView.contentOffset.y;
CGFloat height=collectionView.contentSize.height-CGRectGetHeight(collectionView.frame);
返回Y偏移/高度>kNewPageLoadScrollPercentageThreshold;
}
#pragma标记流布局
-(CGSize)collectionView:(UICollectionView*)collectionView布局:(UICollectionViewLayout*)collectionViewLayout大小FormiteIndeXPath:(NSIndexPath*)indexPath
{
如果(self.feeds.count>0){
PostModel*feed=self.feed[indexPath.row];
return[self.collectionView ar_sizeforcell with identifier:@“DynamicHeightCell”
indepath:indepath
fixedWidth:屏幕宽度配置:^(id单元格){
[细胞填充饲料:饲料];
}];
}
}
//在实现块内部:
#pragma标记-UICollectionViewDelegate
-(无效)collectionView:(UICollectionView*)collectionView
DiEndDisplayingCell:(UICollectionViewCell*)单元格
forItemAtIndexPath:(NSIndexPath*)indexPath
{
[Helper showHUDWithStatus:@“加载下一页”阻止:是];
BOOL shoulldloadnextpage=shoulldloadnextpage(collectionView);
如果(应加载下一页&!页面加载){
[自动加载下一页];
}
}
-(无效)加载下一页{
sectionNum=sectionNum+1;
页码=页码+1;
NSString*页=[NSString stringWithFormat:@“%d”,页码];
NSDictionary*params=[NSDictionary Dictionary WithObjectsSandKeys:page,@“page”,nil];
[PruNSURLSession getUrl:API_GETFEED params:params success:^(NSData*_NullableData,NSURLResponse*_NullableResponse){
n错误*错误;
NSMutableArray*json=[NSJSONSerialization JSONObjectWithData:data
选项:针织品
错误:&错误];
如果(!错误){
for(json格式的NSDictionary*dict){
n错误*错误;
PostModel*post=[[PostModel alloc]initWithDictionary:dict error:&error];
如果(!错误){
[self.feeds addObject:post];
}
}
如果(json.count>0){
NSLog(@“计数:%lu”,(无符号长)self.feeds.count);
NSLog(@“节数:%d”,节数);
dispatch\u async(dispatch\u get\u main\u queue()^{
NSIndexPath*indexPath=[NSIndexPath indexPathForRow:self.feeds.count-1分类:0];
[self.collectionView性能更新:^{
[self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
[self.collectionView insertitemsatindexpath:@[indexPath]];
}完成:^(布尔完成){
[帮手隐藏];
}];
NSLog(@“%lu”,(无符号长)self.feeds.count);
});
}
}
否则{
}
}失败:^(NSData*\u可空数据,NSURLResponse*\u可空响应,NSError*\u可空错误){
NSLog(@“%@”,错误);
}
];
}
#pragma标记-数据源
-(NSInteger)collectionView中的节数:(UICollectionView*)collectionView
{
NSLog(@“%d”,sectionNum);
返回1;
}
-(NSInteger)collectionView:(UICollectionView*)collectionView项目编号截面:(NSInteger)截面
{
如果(self.feeds.count>0){
返回self.feeds.count;
}
返回0;
}
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
如果(self.feeds.count>0){
DynamicHeightCell*cell=[self.collectionView dequeueReusableCellWithReuseIdentifier:@“DynamicHeightCell”forindexath:indexath];
PostModel*feed=self.feed[indexPath.row];
[细胞填充饲料:饲料];
返回单元;
}
返回零;
}
#pragma-mark-funcs
-(void)fetchFeedApi{
NSString*页=[NSString stringWithFormat:@“%d”,页码];
NSDictionary*params=[NSDictionary Dictionary WithObjectsSandKeys:page,@“page”,nil];
[MYNSURLSession getUrl:API_GETFEED参数:参数成功:^(NSData*(可为空的数据),NSURLRResponse*(可为空的响应)){
n错误*错误;
NSMutableArray*json=[NSJSONSerialization JSONObjectWithData:data
@interface feedCollection ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, assign) BOOL onlyImage;

@property (nonatomic, strong) NSMutableArray *feeds;
- (IBAction)addFeed:(id)sender;
- (IBAction)changeLayoutDirection:(id)sender;

@end

static const CGFloat kNewPageLoadScrollPercentageThreshold = 0.66;

@implementation feedCollection{
    CGFloat screenWidth;
    int pageNumber;
    int sectionNum;
    bool pageLoading;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    pageNumber = 0;
    sectionNum = 1;

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    screenWidth = screenRect.size.width;

    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    // Do any additional setup after loading the view, typically from a nib.

    self.feeds = [[NSMutableArray alloc]init];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self fetchFeedApi];
}

static BOOL ShouldLoadNextPage(UICollectionView *collectionView)
{
    CGFloat yOffset = collectionView.contentOffset.y;
    CGFloat height = collectionView.contentSize.height - CGRectGetHeight(collectionView.frame);
    return yOffset / height > kNewPageLoadScrollPercentageThreshold;
}

#pragma mark - flowlayout

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.feeds.count > 0){
        PostModel *feed = self.feeds[indexPath.row];
        return [self.collectionView ar_sizeForCellWithIdentifier:@"DynamicHeightCell"
                                                       indexPath:indexPath
                                                      fixedWidth:screenWidth configuration:^(id cell) {
                                                          [cell filleCellWithFeed:feed];
                                                      }];
    }

}

//  Inside the implementation block:
#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView
  didEndDisplayingCell:(UICollectionViewCell *)cell
    forItemAtIndexPath:(NSIndexPath *)indexPath
{
    [Helper showHUDWithStatus:@"Loading Next Page" blocking:YES];
    BOOL shouldLoadNextPage = ShouldLoadNextPage(collectionView);
    if (shouldLoadNextPage && !pageLoading) {
        [self loadNextPage];
    }
}

- (void)loadNextPage {
    sectionNum = sectionNum + 1;
    pageNumber = pageNumber + 1;
    NSString* page = [NSString stringWithFormat:@"%d", pageNumber];
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:page,@"page", nil];

    [PruNSURLSession getUrl:API_GETFEED params:params success:^(NSData * _Nullable data, NSURLResponse * _Nullable response) {
        NSError* error;
        NSMutableArray* json = [NSJSONSerialization JSONObjectWithData:data
                                                               options:kNilOptions
                                                                 error:&error];
        if(!error){
            for (NSDictionary * dict in json) {
                NSError *error;
                PostModel * post = [[PostModel alloc] initWithDictionary:dict error:&error];
                if(!error){
                    [self.feeds addObject:post];

                }

            }

            if(json.count >0){
                NSLog(@"count : %lu",(unsigned long)self.feeds.count);
                NSLog(@"sections : %d",sectionNum);
                dispatch_async(dispatch_get_main_queue(), ^{
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.feeds.count-1 inSection:0];
                    [self.collectionView performBatchUpdates:^{
                        [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
                        [self.collectionView insertItemsAtIndexPaths:@[indexPath]];
                    } completion:^(BOOL finished) {
                        [Helper hideHUD];

                    }];

                    NSLog(@"%lu",(unsigned long)self.feeds.count);

                });
            }


        }
        else {

        }

    } failure:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"%@",error);
    }
     ];





}

#pragma mark - dataSources

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    NSLog(@"%d", sectionNum);
    return 1;


}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.feeds.count > 0 ){
      return   self.feeds.count;
    }
    return 0;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
 if(self.feeds.count > 0){
        DynamicHeightCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"DynamicHeightCell" forIndexPath:indexPath];
        PostModel *feed = self.feeds[indexPath.row];
        [cell filleCellWithFeed:feed];

        return cell;
 }
    return nil;

}

#pragma mark - funcs



- (void)fetchFeedApi {

    NSString* page = [NSString stringWithFormat:@"%d", pageNumber];
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:page,@"page", nil];

    [MYNSURLSession getUrl:API_GETFEED params:params success:^(NSData * _Nullable data, NSURLResponse * _Nullable response) {
        NSError* error;
        NSMutableArray* json = [NSJSONSerialization JSONObjectWithData:data
                                                               options:kNilOptions
                                                                 error:&error];
        if(!error){
            for (NSDictionary * dict in json) {
                NSError *error;
                PostModel * post = [[PostModel alloc] initWithDictionary:dict error:&error];
                if(!error){
                    [self.feeds addObject:post];
                }

            }

            if(json.count >0){

                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    [self.collectionView reloadData];

                }];

            }


        }
        else {

        }

    } failure:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"%@",error);
    }
     ];



}
@end