ios - UICollectionView 内置的重新排序对于不同大小的单元格无法正常工作

标签 ios uicollectionview

UICollectionView在 iOS 9 中添加了对重新排序单元格的支持,但如果所有单元格的大小不同,实时交互将无法正常工作:

请注意,在此屏幕截图中,交换具有相同大小的单元格 #3 和 #2 如何正常工作,而对于单元格 #3 和 #1 则不能:

enter image description here

基本上它似乎是 UICollectionView移动选定单元格时不会重新计算其他单元格的布局。有谁知道解决方法?

另一个相关问题是,当不同大小的单元格重新排序完成后,您需要手动使 UICollectionView 无效。布局,否则某些单元格的位置无效并重叠。

PS:我正在使用长按手势识别器并根据需要调用这些方法:

- (BOOL)beginInteractiveMovementForItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0); // returns NO if reordering was prevented from beginning - otherwise YES
- (void)updateInteractiveMovementTargetPosition:(CGPoint)targetPosition NS_AVAILABLE_IOS(9_0);
- (void)endInteractiveMovement NS_AVAILABLE_IOS(9_0);
- (void)cancelInteractiveMovement NS_AVAILABLE_IOS(9_0);

最佳答案

对于有这个问题的人来说,原因是UICollectionView拖动时不会重新排列项目的布局属性。

我的意思是,如果项目 #2 是 50x50,项目 #3 是 100x100,在实时拖动项目并交换它们时,UICollectionView也不会自动交换它们的大小。位置 2 处的任何内容始终假定为 50x50,位置 3 处的任何内容始终假定为 100x100。

解决方案是确保在移动项目时,UICollectionView数据源不断更新以反射(reflect)新的位置。最好的方法似乎是在 UICollectionView委托(delegate)方法:

- (NSIndexPath *)collectionView:(UICollectionView *)collectionView
       targetIndexPathForMoveFromItemAtIndexPath:(NSIndexPath *)originalIndexPath
       toProposedIndexPath:(NSIndexPath *)proposedIndexPath

关于ios - UICollectionView 内置的重新排序对于不同大小的单元格无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38517883/

相关文章:

iphone - 找到第一响应者的 View

ios - UIAlertController 不会显示 - 在 Swift 中

IOS Swift 读取 PCM Buffer

ios - swift iOS : Memory leaks with the following code (video player)

ios - 如何使用 UICollectionView 的 selectItem 在 collectionView 单元格上绘制?

ios - 如何根据collectionView中的条件隐藏textview及其高度?

ios - UICollectionViewCell 拖动动画

android - android和ios有多少个端口号?

swift - UICollectionView 重复单元格

ios - UICollectionView 渲染得比 Storyboard 中的要高