Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS中CollectionView中的单元格重新排序_Ios_Objective C_Uicollectionview - Fatal编程技术网

iOS中CollectionView中的单元格重新排序

iOS中CollectionView中的单元格重新排序,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我正在尝试对collectionView中的单元格重新排序,但未调用collection View delgate方法。 我已正确设置了代理 (void)collectionView:(UICollectionView*)collectionView moveItemAtIndexPath:(NSIndexPath)sourceIndexPath到IndeXPath:(NSIndexPath)destinationIndexPath 这是我的工作代码 - (void)viewDidLoad

我正在尝试对collectionView中的单元格重新排序,但未调用collection View delgate方法。 我已正确设置了代理

  • (void)collectionView:(UICollectionView*)collectionView moveItemAtIndexPath:(NSIndexPath)sourceIndexPath到IndeXPath:(NSIndexPath)destinationIndexPath
这是我的工作代码

- (void)viewDidLoad {
    [super viewDidLoad];
    UILongPressGestureRecognizer *longGesture=[[UILongPressGestureRecognizer alloc] init];
    [longGesture addTarget:self action:@selector(handleGesture:)];
    [self.collectionView addGestureRecognizer:longGesture];
    NSLog(@"The array is %@",dataArray);
    self.collectionView.delegate=self;
    [_collectionView reloadData];
    // Do any additional setup after loading the view, typically from a nib.
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [dataArray count];
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.cellImageView.image = [dataArray objectAtIndex:indexPath.row];;

    return cell;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}


- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath{
    UIImage *sourceImage = [dataArray objectAtIndex:sourceIndexPath.row];
    UIImage *destImage =  [dataArray objectAtIndex:destinationIndexPath.row];
    [arr insertObject:destImage atIndex:sourceIndexPath.row];
    [arr insertObject:sourceImage atIndex:destinationIndexPath.row];



}

-(void)handleGesture:(UILongPressGestureRecognizer*)gesture{
    switch (gesture.state) {
        case UIGestureRecognizerStateBegan:{
            NSIndexPath *indexPath=[self.collectionView indexPathForItemAtPoint:[gesture locationInView:self.collectionView]];
            if(indexPath!=nil)
                [self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
            break;
        }
        case UIGestureRecognizerStateChanged:{
            [self.collectionView updateInteractiveMovementTargetPosition:[gesture locationInView:self.collectionView]];
        }
        case UIGestureRecognizerStateEnded:{

            [self.collectionView endInteractiveMovement];
        }

        default:
            [self.collectionView cancelInteractiveMovement];
            break;
    }
}

我想您忘了设置
数据源
,所以在
视图加载
中添加以下行

 self.collectionView.datasource = self;
确保你已经确认了两个协议

  <UICollectionViewDataSource,UICollectionViewDelegate>

在每个case语句中添加一个中断,您的代码就会正常运行


这可能是将Swift示例代码翻译成Objective C的结果。

@ChiragPatel:您应该在原始帖子中添加此评论,而不是答案!!