UICollectionView reloadItemsAtIndexPaths

I am trying to wrap my head around the reloadItemsAtIndexPaths UICollectionView method.

I have an array of objects called objectsArray. When the user scrolls to the bottom of my collection view, I select the next group of objects from the backend and add it to the Array object by calling [objectsArray addObjectsFromArray: objects]; After that, I call [self.collectionView reloadData], which, as I know, is expensive.

I would like to optimize the code below, but I get an assertion error when calling reloadItemsAtIndexPaths.

    if (self.searchPage == 0) {
        parseObjectsArray = [[NSMutableArray alloc] initWithArray:relevantDeals];
        [self.collectionView reloadData];

    } else {

        NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
        for (int i = 0; i < [relevantDeals count]; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
            [indexPaths addObject:indexPath];
        }

        [parseObjectsArray addObjectsFromArray:relevantDeals];
        [self.collectionView reloadItemsAtIndexPaths:indexPaths];
        //[self.collectionView reloadData];
    }

Error: * Approval error in - [UICollectionView _endItemAnimations], / SourceCache / UIKit / UIKit-2903.2 / UICollectionView.m: 3716

Any help / advice is appreciated.

Thanks!

+4
3

, , , , . reloadItemsAtIndexPaths: indexPaths insertItemsAtIndexPaths:

[self.collectionView insertItemsAtIndexPaths:indexPaths]; // Use this for newly added rows
//[self.collectionView reloadItemsAtIndexPaths:indexPaths]; // Use this for existing rows which have changed
+5

. ,

            @try
            {
                [self.collectionView insertItemsAtIndexPaths:indexPaths];
            }
            @catch (NSException *except)
            {
                NSLog(@"DEBUG: failure to insertItemsAtIndexPaths.  %@", except.description);
            }

, indexPaths. , , , numberOfItemsInSection.

, !

+3

This error often occurs when increasing / decreasing the number of elements in an array, but it does not match the data source method

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

Are you updating it accordingly?

0
source

All Articles