objective-c – 是否可以在使用UICollectionViewFlowLayout的UICollectionView中交换2个单元格?

说我有一个UICollectionView使用UICollectionViewFlowLayout和以下格式:
1 2 3
4 5 6
7 8 9

我触摸单元格9并将其拖动到单元格6上,将单元格9移动到单元格6的单元格和单元格6到单元格9的位置:

1 2 3
4 5 9
7 8 6

有没有办法轻松移动和动画单元格6到单元格9以及移动和动画单元格9到单元格6的位置?还是我必须将UICollectionViewLayout子类化?如果我必须对UICollectionViewLayout进行子类化,那该怎么做?我已经尝试明确设置单元格的中心

– (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath

但没有成功.

解决方法

经过很多的敲击,我发现的解决方案是使用moveItemAtIndexPath:toIndexPath里面的performBatchUpdates:completion.根据 documentation for performBatchUpdates:完成:

You can use this method in cases where you want to insert,delete,reload or move cells around the collection view in one single animated operation,as opposed to in several separate animations. Use the blocked passed in the updates parameter to specify all of the operations you want to perform.

所以,我本来做的是什么

[self performBatchUpdates:^{
    [self moveItemAtIndexPath:fromIp toIndexPath:toIp];
    [self moveItemAtIndexPath:toIp toIndexPath:fromIp];
} completion:^(BOOL finished) {
    // update data source here
    // e.g [dataSource exchangeObjectAtIndex:fromIp.row withObjectAtIndex:toIp.row];
}];

相关文章

首先GDB是类unix系统下一个优秀的调试工具, 当然作为debug代...
1. C语言定义1个数组的时候, 必须同时指定它的长度.例如:int...
C++的auto关键字在C+⬑新标准出来之前基本...
const关键字是用于定义一个不该被改变的对象,它的作用是告诉...
文章浏览阅读315次。之前用C语言编过链表,这几天突然想用C+...
文章浏览阅读219次。碰到问题就要记录下来,防止遗忘吧。文章...