我无法为 a 中的部分之间的更改设置动画UICollectionView
,我的程序不断崩溃,它有什么问题?
我有一个包含四个部分的集合视图:
0:A
1:B
2:C
3:D
我想将其转换为只有三个具有相同项目的部分:
0:A
1:B、C
2:D
我想动画这个转换:
// Initial state
NSArray *source = @[ @[@"A"], @[@"B"], @[@"C"], @[@"D"] ];
// Some data source methods
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [source[section] count];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return [source count];
}
// Transformation: that works but I have to keep an empty section
source = @[ @[@"A"], @[@"B", @"C"], @[@"D"], @[] ];
[collection performBatchUpdates:^{
[collection moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]
toIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
[collection moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]
toIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]];
[collection moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:2]
toIndexPath:[NSIndexPath indexPathForItem:1 inSection:1]];
[collection moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:3]
toIndexPath:[NSIndexPath indexPathForItem:0 inSection:2]];
} completion:nil];
// Transformation: that crashes!
source = @[ @[@"A"], @[@"B", @"C"], @[@"D"] ];
[collection performBatchUpdates:^{
[collection moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]
toIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
[collection moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]
toIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]];
[collection moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:2]
toIndexPath:[NSIndexPath indexPathForItem:1 inSection:1]];
[collection moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:3]
toIndexPath:[NSIndexPath indexPathForItem:0 inSection:2]];
[collection deleteSections:[NSIndexSet indexSetWithIndex:3]];
} completion:nil];
我一直在崩溃,要么是内部断言失败:Assertion failure in -[UICollectionView _endItemAnimations]...
,要么有时,更奇怪的是,一个 malloc 错误:incorrect checksum for freed object...
。
如果我不打电话deleteSections:
,它也不起作用。如果我把它放在第一位,它不会改变任何事情。如果我删除moveItemAtIndexPath:toIndexPath:
具有相同源和目标的那些,它不会改变任何东西。如果我不在批处理块中执行此操作,它显然会在第一个命令时崩溃。我忽略了什么吗?