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

时间:2015-11-09 07:22:36

标签: 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");
}

2 个答案:

答案 0 :(得分:11)

您在 switch-case 块中缺少break;个语句。如果每个 case 部分末尾没有break;,则不会退出switch-case语句,并执行下一行。

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

为嵌入UICollectionView内的UICollectionViewController 而不是实施重新排序(可能改为UIViewController内部,但可能位于UICollectionViewCell或{ {1}}如果你想得到的话,至少需要以下三个部分:

  1. UITableViewCell内(或者,如果您将viewDidLoad嵌入到故事板中设置的UICollectionViewUITableViewCell内,则此代码进入UICollectionViewCell),向awakeFromNib添加UILongPressGestureRecognizer,如下所示:

    UICollectionView
  2. 实施UILongPressGestureRecognizer *longGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:) [self.yourCollectionView addGestureRecognizer:longGR]; 以对刚刚添加的handleLongPress:做出反应:

    UILongPressGestureRecognizer
  3. 最后,添加以下内容:

    - (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;
            }
        }
    }
    
  4. ....当你触摸,按住然后拖动时,细胞现在会神奇地移动!

    (这都假设您已实施标准- (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 } dataSource 委托方法,并且UICollectionView已成功加载/在您的应用中显示。如果您需要有关UICollectionView基本设置的帮助,请查看Warewolf的this post

答案 1 :(得分:0)

您的switch语句正在落空(将使用Swift编写的博客文章翻译成Objective-C的风险之一)。每次调用处理程序时触摸位置的变化都会执行Changed:case下面的每个语句,包括cancelInteractiveMovement(另一个注释,应该是endInteractiveMovement)。