刷新collectionView时,它会崩溃,因为它不会更新numberOfItemsInSection

时间:2015-12-22 03:22:10

标签: ios objective-c parse-platform uicollectionview

当用户下拉时我有刷新方法,似乎它没有更新数组计数而只是崩溃:

以下是崩溃:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 9 beyond bounds [0 .. 8]'

这是我的代码:

查询数据:(也用于刷新)

- (void) queryData
{
    PFObject *queryPhotos = self.userInformationObject;
    PFQuery *query = [PFQuery queryWithClassName:@"Photo"];
    [query whereKey:@"userTookPhoto" equalTo:queryPhotos];
    [query orderByAscending:@"createdAt"];
    [query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
        if (!error) {
            imageFilesArray = [[NSArray alloc] initWithArray:objects];
            [self.myCollectionView reloadData];

            [self.refreshControl endRefreshing];
        }
    }];
}

CellForItemAtIndexPath

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * const reuseIdentifier = @"imageCell";
    imageCollectionCell *cell = [self.myCollectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    // Configure the cell



    cell.layer.borderColor = [UIColor grayColor].CGColor;
    cell.layer.borderWidth = 0.5;
    cell.layer.cornerRadius = 7.0f;
    cell.clipsToBounds = YES;


    PFObject *imageObject = [imageFilesArray objectAtIndex:indexPath.row];

    PFFile *imageFile = [imageObject objectForKey:@"image"];
    [imageFile getDataInBackgroundWithBlock:^(NSData * _Nullable data, NSError * _Nullable error) {
        if (!error) {

            cell.imageViewCell.image = [UIImage imageWithData:data];
        }
        else{
            NSLog(@"Fail");
        }
    } progressBlock:^(int percentDone) {

    }];
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    //#warning Incomplete implementation, return the number of sections
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    //#warning Incomplete implementation, return the number of items
    return [imageFilesArray count];
}

这是与刷新相关的所有代码,并显示单元格,我不知道哪里出错了,希望有人有答案。 提前致谢。

1 个答案:

答案 0 :(得分:0)

尝试以下代码。

- (void) queryData
{

   [self.refreshControl endRefreshing];

    PFObject *queryPhotos = self.userInformationObject;
    PFQuery *query = [PFQuery queryWithClassName:@"Photo"];
    [query whereKey:@"userTookPhoto" equalTo:queryPhotos];
    [query orderByAscending:@"createdAt"];
    [query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
        if (!error) {
            imageFilesArray = [[NSArray alloc] initWithArray:objects];
        }
    }];
    [self.myCollectionView reloadData];
}