gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 05:50:09 26 4
gpt4 key购买 nike

我正在尝试对 collectionView 中的单元格进行重新排序,但是没有调用 collectionView delgate 方法。我已正确设置委托(delegate)。

  • (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;
}
}

最佳答案

为每个 case 语句添加一个 break,您的代码将正常运行!

这个陷阱可能是将 Swift 示例代码转换为 Objective C 的结果。

关于ios - iOS CollectionView 中的单元格重新排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38916737/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com