【发布时间】:2013-05-24 13:57:51
【问题描述】:
我在为 UICollectionView 中的部分之间设置动画时遇到问题,我的程序不断崩溃,这是怎么回事?
我有一个包含四个部分的集合视图:
0: A
1:乙
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:,它不会改变任何东西。如果我不在批处理块中执行此操作,它显然会在第一个命令时崩溃。
我是不是忽略了什么?
【问题讨论】:
-
您找到解决方案了吗?似乎在同一批更新中使用 moveItemAtIndexPath:toIndexPath: 和 deleteSections: 总是会导致崩溃,但需要确认。
标签: ios crash uicollectionview