C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了objective-c – UICollectionView重新排序动画大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在制作一个包含所有基本内容的集合视图,例如认的FlowLayout,独特的部分.假设我正确使用COllectionView:CellForItemATindexPath:以及所有其他datasource协议

@L_272_3@mainData,它是一个字典数组(原始数据),mainDataReferenceordered,mainDatanameordered和mainDataQuantiteordered是包含与mainData相同数据的其他数组(指向相同的元素).
dataToDisplay是控制器当前指向有序数据的数组指针.

重新排序时,我只是更改集合视图批处理中的数据,如下所示:

[itemCollectionControl.collectionView performBatchupdates:^{
        dataToDisplay = mainDataReferenceordered; //or any other ordered array
        [itemCollectionControl.collectionView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NsmakeRange(0,itemCollectionControl.collectionView.numberOfSections)]];
    } completion:^(BOOL finished) {}];

但是,即使它们已经可见,或者在正确的位置,该集合也会消失所有细胞.

我读了Apple documentation on UICollectionView,但我不知道我错过了什么.
我还阅读了other threads,但仍在寻找我必须做的事情.

批处理看起来知道要在单元格上应用哪个动画?

我的解决方

这是我使用的最终代码,当然是我最接近的iOS编程指南.

[itemCollectionControl.collectionView performBatchupdates:^{
    NSArray *oldOrder = dataToDisplay;
    dataToDisplay = mainDatanBContainersordered;
    for (NSInteger i = 0; i < oldOrder.count; i++) {
        NSIndexPath *fromIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
        NSInteger j = [dataToDisplay indexOfObject:oldOrder[i]];
        NSIndexPath *toIndexPath = [NSIndexPath indexPathForRow:j inSection:0];
        [itemCollectionControl.collectionView moveItemATindexPath:fromIndexPath toIndexPath:toIndexPath];
    }
} completion:^(BOOL finished) {
    if (finished) {
        [itemCollectionControl.collectionView reloadData];
    }
}];

我只是浏览所有旧的有序项目,检查它们的新位置,并将其应用于 – [UICollectionView moveItemATindexPath:toIndexPath:]
还有一些重复的细胞故障,但它在iOS7上看起来很好(不在iOS6上).
在iOS7上完成可能没用,我在iOS6改组的最后强制执行正确的顺序.

编辑

我想我找到了一个解决方案,但我无法再对该项目进行测试.也许只添加2行代码就可以解决这个可怕的故障.

调用之前 – [UICollectionView moveItemATindexPath:toIndexPath:],调用 – [UICollectionView beginupdates],最后在所有移动之后 – [UICollectionView endupdates].

如果有人能够测试它发现它有效,请告诉我.

解决方法

集合视图不知道数据模型中项目的标识,因此无法知道它们已移动.所以你必须使用moveItemATindexPath:toIndexPath:明确告诉集合视图每个单元格在批量更新中的位置.您可以通过循环遍历from数组中的项目并在to数组中查找它们的位置来自行计算.像这样的东西(从记忆中输入,对任何拼写错误都很抱歉):
[itemCollectionControl.collectionView performBatchupdates:^{
    for (NSInteger i = 0; i < mainDataReferenceordered.count; i++) {
        NSIndexPath *fromIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
        NSInteger j = [mainDatanameordered indexOfObject:mainDataReferenceordered[i]];
        NSIndexPath *toIndexPath = [NSIndexPath indexPathForRow:j inSection:0];
        [itemCollectionControl.collectionView moveItemATindexPath:fromIndexPath toIndexPath:toIndexPath];
    }
} completion:^(BOOL finished) {}];

如果您有很多(数千个)项目,您可能需虑使用集合来加快查找速度.

更普遍适用的方法是使用类似TLIndexPathTools的东西,可以为您计算批量更新.看看Shuffle sample project.

大佬总结

以上是大佬教程为你收集整理的objective-c – UICollectionView重新排序动画全部内容,希望文章能够帮你解决objective-c – UICollectionView重新排序动画所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。