Collection items will not reload after moving

I use UICollectionView to display players in different positions on the field. Each cell contains labels showing the name of the player and the name of the position. The location in the view is determined by a custom layout that reads location information from a data source ... i.e. The location in the field has nothing to do with the index in the data source, as in a stream layout. Cells are also slightly different for different positions ... 'cellForItemAtIndexPath' selects from different reuse identifiers depending on the position.

I want the user to be able to change players in positions. Everything works fine while I just update the collection view data source and call [self.collectionView reloadData] . But I want to animate the swap cells so that the user can see the changes. I use this code where inboundSourceIndex and outboundSourceIndex were previously set to elements that change:

 [self.collectionView performBatchUpdates:^{ [self.collectionView moveItemAtIndexPath:inboundOriginIndex toIndexPath:outboundOriginIndex]; [self.collectionView moveItemAtIndexPath:outboundOriginIndex toIndexPath:inboundOriginIndex]; } completion:nil]; } [self assignmentsUpdate]; // This method updates the data source for the collection view [self.collectionView.collectionViewLayout invalidateLayout]; [self.collectionView reloadData]; 

After I insert the batch update, I get the desired animation of the cells, which apparently change with each other, the player’s name labels are displayed correctly, and the layout provides the correct frames for the cells (which can vary). But everything else is chaos: position labels are not updated, and cells are not reloaded with their correct reuse identifiers - it looks like collectionView does not actually call cellForItemAtIndexPath . I tried changing the order of data refresh and reloadData, but to no avail. I can see how moving cells can temporarily change indexPaths, but how can CollectionView look wrong after calling invalidateLayout and reloadData ? Does this create a view from scratch based on the data source?

+6
source share

All Articles