OGeek|极客世界-中国程序员成长平台

标题: ios - UICollectionView 移动项目并保持单元格大小 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:49
标题: ios - UICollectionView 移动项目并保持单元格大小

我有不同大小的单元格,我正在尝试移动 CollectionView 单元格,并保持移动/选定的单元格大小移动到新位置。

我已经制作了project在 Github 上公开,如果您想被添加为合作者,请告诉我?

我是子类 UICollectionViewFlowLayout 并实现了以下内容:

- (UICollectionViewLayoutInvalidationContext *)invalidationContextForInteractivelyMovingItemsNSArray<NSIndexPath *> *)targetIndexPaths withTargetPositionCGPoint)targetPosition previousIndexPathsNSArray<NSIndexPath *> *)previousIndexPaths previousPositionCGPoint)previousPosition{

    UICollectionViewLayoutInvalidationContext *context = [super
                                                          invalidationContextForInteractivelyMovingItems:targetIndexPaths
                                                          withTargetPosition:targetPosition
                                                          previousIndexPaths:previousIndexPaths
                                                          previousPosition:previousPosition];

    [self.collectionView moveItemAtIndexPath:previousIndexPaths[0] toIndexPath:targetIndexPaths[0]];

    return context;
}

也尝试在 UICollectionViewController 中实现这一点:

-(NSIndexPath *)collectionViewUICollectionView *)collectionView targetIndexPathForMoveFromItemAtIndexPathNSIndexPath *)originalIndexPath toProposedIndexPathNSIndexPath *)proposedIndexPath{
    if (originalIndexPath.section == proposedIndexPath.section) {
        return  proposedIndexPath;
    }else{
        return originalIndexPath;
    }
}

你知道如何保持尺寸吗?



Best Answer-推荐答案


我遇到了同样的问题,原因是您需要调用委托(delegate)的 moveItemAtIndexPath。

- (UICollectionViewLayoutInvalidationContext *)invalidationContextForInteractivelyMovingItemsNSArray<NSIndexPath *> *)targetIndexPaths withTargetPositionCGPoint)targetPosition previousIndexPathsNSArray<NSIndexPath *> *)previousIndexPaths previousPosition:(CGPoint)previousPosition{

    UICollectionViewLayoutInvalidationContext *context = [super
                                                          invalidationContextForInteractivelyMovingItems:targetIndexPaths
                                                          withTargetPosition:targetPosition
                                                          previousIndexPaths:previousIndexPaths
                                                          previousPosition:previousPosition];

    [self.collectionView.dataSource collectionView:self.collectionView moveItemAtIndexPath:previousIndexPaths[0] toIndexPath:targetIndexPaths[0]];

    return context;
}

关于ios - UICollectionView 移动项目并保持单元格大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39977555/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4