在部分之间重新排列项目时,UICollectionView会崩溃

Gui*_*ume 5 crash ios uicollectionview

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

我一直在崩溃,要么内部断言失败Assertion failure in -[UICollectionView _endItemAnimations]...,要么有时甚至更奇怪,一个malloc错误:incorrect checksum for freed object....

如果我不打电话deleteSections:它也不起作用.如果我先放,它不会改变任何东西.如果我删除moveItemAtIndexPath:toIndexPath:具有相同源和目标的那个,它不会改变任何东西.如果我不在批处理块中执行它,它显然会在第一个命令崩溃.我忽略了什么吗?

Mik*_*ard 0

你有没有尝试过类似的事情:

source = @[ @[@"A"], @[@"B", @"C"], @[@"D"] ];

[collection performBatchUpdates:^{
    [collection moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:2]
                        toIndexPath:[NSIndexPath indexPathForItem:1 inSection:1]];
    [collection deleteSections:[NSIndexSet indexSetWithIndex:2]];
} completion:nil];
Run Code Online (Sandbox Code Playgroud)