移动后,集合视图单元格不会重新加载

Hen*_*y95 7 batch-updates reloaddata ios uicollectionview uicollectionviewlayout

我正在使用a UICollectionView来显示场上不同位置的球员.每个单元格都包含显示玩家姓名和位置名称的标签.视图中的位置由自定义布局确定,该布局从数据源读取位置信息...即,字段上的位置与数据源中的索引无关,如在流布局中.对于各种位置,单元格也有点不同......'cellForItemAtIndexPath'根据位置从各种重用标识符中进行选择.

我希望用户能够在位置上交换玩家.只要我更新集合视图数据源并调用,一切都可以正常工作[self.collectionView reloadData].但我想动画单元格的交换,以便用户可以看到更改.我正在使用此代码,其中inboundSourceIndex和outboundSourceIndex先前已设置为交换的项目:

[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];
Run Code Online (Sandbox Code Playgroud)

插入批量更新后,我得到所需的单元格动画,它们之间相互交换,播放器名称标签显示正确,并且布局为单元格提供了正确的帧(可能会有所不同).但其他一切都很混乱:位置标签没有更新,并且单元格没有使用正确的重用标识符重新加载 - 就像collectionView没有真正调用一样cellForItemAtIndexPath.我已经尝试更改数据更新和reloadData的顺序,但无济于事.我可以看到移动的细胞可能暂时改变indexPaths,但的CollectionView怎么看待错误后,我打电话invalidateLayoutreloadData?是不是基于数据源从头开始构建视图?