UICollection VIew断言失败

时间:2017-05-01 13:22:55

标签: objective-c uicollectionview assertions

- (void)loadNextPage失败了。

  

断言失败 - [UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:]

     

无法向第0部分插入行。或者到新的部分。   我每次调用api时都试图插入新的单元格。   json每页返回5个元素因此pagecount。

@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

0 个答案:

没有答案