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

5

我正在尝试重新排列collectionView中的单元格,但是集合视图委托方法没有被调用。

我已经正确设置了代理。

  • (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath )sourceIndexPath toIndexPath:(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;
    }
}
2个回答

2

在每个case语句中添加一个break,你的代码就可以正常运行了!

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


0

我认为您忘记设置datasource了,因此请在viewDidload中添加以下行:

 self.collectionView.datasource = self;

确保您已确认两个协议,

  <UICollectionViewDataSource,UICollectionViewDelegate>

2
@ChiragPatel:你应该在原帖上添加这个评论,而不是回答! - Ketan Parmar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接