ios - 无法通过移动它们来重新排列 UICollectionView 中的单元格?

标签 ios objective-c iphone ipad

我有 UICollectionView 我在其上显示 定制 细胞。我试图通过长按手势来移动细胞,但它根本不起作用。

我关注了this教程,但它对我不起作用。请告诉我该怎么做?

长按手势

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{

    NSLog(@"long press geture called");
    switch(gestureRecognizer.state)
    {
        case UIGestureRecognizerStateBegan:
            p = [gestureRecognizer locationInView:self.collection_view];
          gesture_indexPath = [self.collection_view indexPathForItemAtPoint:p];
        [self.collection_view beginInteractiveMovementForItemAtIndexPath:gesture_indexPath];
            NSLog(@"state began called");

                     break;

        case UIGestureRecognizerStateChanged:

        //    collectionView.updateInteractiveMovementTargetPosition(gesture.locationInView(gesture.view!))
            [self.collection_view updateInteractiveMovementTargetPosition:[gestureRecognizer locationInView:[gestureRecognizer view]]];
            NSLog(@"state changed called");
        case UIGestureRecognizerStateEnded:
            [self.collection_view updateInteractiveMovementTargetPosition:p];
                  NSLog(@"state changed called");
        default:
            [self.collection_view cancelInteractiveMovement];
    }
}

方法覆盖
-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSLog(@"Move at index path called");
}

最佳答案

你错过了break; switch-case block 中的语句。没有 break;在每个 case 部分的末尾,不退出 switch-case 语句,而是执行下一行。

为了完整起见,这里是 Objective-C 中用于可重新排序的 UICollectionView 的最低实现。 .

UICollectionView 实现重新排序不是 嵌入到 UICollectionViewController (可能是在 UIViewController 内,但也可能在 UICollectionViewCellUITableViewCell 内,如果你喜欢的话)至少需要以下三个部分:

  • 内部 viewDidLoad (或者,如果您将 UICollectionView 嵌入到 UITableViewCellUICollectionViewCell 在 Storyboard 中设置,则此代码将进入 awakeFromNib ),添加 UILongPressGestureRecognizer给您的UICollectionView像这样:
    UILongPressGestureRecognizer *longGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)
    [self.yourCollectionView addGestureRecognizer:longGR];
    
  • 实现handleLongPress:UILongPressGestureRecognizer 使用react您刚刚添加:
    - (void)handleLongPress:(UILongPressGestureRecognizer *)gr{   
        switch(gr.state){
            case UIGestureRecognizerStateBegan:
            {
                NSIndexPath *selectedIndexPath = [self.yourCollectionView indexPathForItemAtPoint:[gr locationInView:self.yourCollectionView]];
                if(selectedIndexPath == nil) break;
                [self.yourCollectionView beginInteractiveMovementForItemAtIndexPath:selectedIndexPath];
                break;
            }
            case UIGestureRecognizerStateChanged:
            {
                [self.yourCollectionView updateInteractiveMovementTargetPosition:[gr locationInView:gr.view]];
                break;
            }
            case UIGestureRecognizerStateEnded:
            {
                [self.yourCollectionView endInteractiveMovement];
                break;
            }
            default:
            {
                [self.yourCollectionView cancelInteractiveMovement];
                break;
            }
        }
    }
    
  • 最后,添加以下内容:
    - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    
        //add your data source manipulation logic here
        //specifically, change the order of entries in the data source to match the new visual order of the cells.
        //even without anything inside this function, the cells will move visually if you build and run
    
    }
    

  • ....当您触摸、按住、拖动时,细胞现在会神奇地移动!

    (这一切都假设您已经实现了标准的 UICollectionView 数据源和委托(delegate)方法,并且 UICollectionView 已成功加载/显示在您的应用程序中。如果您需要有关 UICollectionView 的基本设置的帮助,请查看this post 作者:狼人)。

    关于ios - 无法通过移动它们来重新排列 UICollectionView 中的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33604209/

    相关文章:

    ios - MapKit 注释未显示在 map 上

    ios - react-native 与现有 ios 应用程序集成

    iphone - __weak UIDataType *weakSelf 和 UIDataType __weak *weakSelf 之间的区别?

    ios - 如何退出 SWRevealViewController?

    iphone - CALayer setNeedsDisplayInRect - InRect 的意义是什么?或者在drawInContext中更快地绘制

    java - 在 android 和 ios 的 flutter 应用程序中应用深度链接

    ios - 从同一组件调用时,两个 IBAction 的触发顺序是什么?

    iphone - opencv框架与opengles trows错误?

    iphone - 从网站订购多个异步 JSON 解析

    iphone - iTunes Connect 销售报告中的负单位数