Ios – UICollectionView is not updating properly – used THStringyFlowLayout

iosobjective cuicollectionview

When my app starts, it takes data from the persistence and displays 5 cells in my Collection View. These cells looks like normal TableView cells, but I use Collection View for later layout tweaks. It takes 8 cells to fill whole screen.

A second after that, the model is updated with the data for 19 cells and the Collection View is updated by calling:

[self.collectionView reloadData];

Those already displayed 5 cells are nicely refreshed with new data, but that's it. The other cells are not displayed until I touch the screen and scroll down about the height of the cell. Then instantly 3 cells are displayed and fills the screen. From that moment, everything is fine, but until I scroll, it is not updated correctly.

When I debugged it, I see, that the collectionView:numberOfItemsInSection: is called after calling reloadData and it returns correct number 19, but the collectionView:cellForItemAtIndexPath: is asked just for those 5 cells (until I scroll).

Kind of desperate here, thanks for your help.

Edit – added source code:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.manager.jubilees count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DLog(@"asked for cell: %i", indexPath.row); //just for debugging to know, what is going on
    JUJubileeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"JubileeCell"
                                                                forIndexPath:indexPath];
    cell.jubilee = self.manager.jubilees[(NSUInteger) indexPath.row];
    return cell;
}

// called when model is updated
-(void)jubileesUpdated {
    DLog(@"jubileesUpdated");
    [self.collectionView reloadData];
}

edit 2:
When first 5 cells are loaded, the [self.collectionView contentSize] is (width=320, height=358).
When the all 19 cells are loaded and reloadData called, contentSize is (width=320, height=1366) – but cells are not actually displayed :(.

edit 3:
The problem is caused by using THStringyFlowLayout – I will post more, if I find the reason.

Best Answer

Try adding all the index paths you want to refresh into an array and try using [_collectionView reloadItemsAtIndexPaths:arayOfAllIndexPaths]