iOS调整collectionViewCell顺序

2024-06-10 19:44

本文主要是介绍iOS调整collectionViewCell顺序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

效果图

请添加图片描述

原理

就是设置collectionView调整顺序的代理方法,这里要注意一点
调整过代理方法之后,一定要修改数据源,否则导致错乱。
还有就是在collectionView上面添加一个长按手势,在长按手势的不同阶段,调用collectionView 调整顺序的系统方法 beginInteractiveMovementForItemAtIndexPath
等等

代码

//
//  ViewController.m
//  LBEditCollectionCellOrder
//
//  Created by mac on 2024/6/10.
//#import "ViewController.h"
#import "LBOrderCell.h"@interface ViewController () <UICollectionViewDelegate, UICollectionViewDataSource>@property (nonatomic, strong) UICollectionView *collectionView;@property (nonatomic, strong) UILongPressGestureRecognizer *longPressRecognizer;@property (nonatomic, strong) NSMutableArray *dataArray;@property (nonatomic, assign) BOOL inDrag;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];[self.view addSubview:self.collectionView];[self.collectionView reloadData];[self.collectionView addGestureRecognizer:self.longPressRecognizer];// Do any additional setup after loading the view.
}#pragma mark - 手势拖拽- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{//最后一个不能拖拽return self.dataArray.count > indexPath.item;
}- (NSIndexPath *)collectionView:(UICollectionView *)collectionView targetIndexPathForMoveOfItemFromOriginalIndexPath:(NSIndexPath *)originalIndexPath atCurrentIndexPath:(NSIndexPath *)currentIndexPath toProposedIndexPath:(NSIndexPath *)proposedIndexPath
{if ([self collectionView:collectionView canMoveItemAtIndexPath:proposedIndexPath]) {return proposedIndexPath;}return originalIndexPath;
}- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{NSString *title = [self.dataArray objectAtIndex:sourceIndexPath.item];if (!title) {return;}NSInteger targetIdx = destinationIndexPath.item;NSLog(@"修改之前的数量%ld",self.dataArray.count);[self.dataArray removeObject:title];NSLog(@"这里的%@", title);NSLog(@"这是否包含%d", [self.dataArray containsObject:title]);[self.dataArray insertObject:title atIndex:targetIdx];NSLog(@"修改之后的数量%ld", self.dataArray.count);
}#pragma mark - UICollectionViewDelegate, UICollectionViewDataSource- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{return 1;
}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{return self.dataArray.count;
}- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{LBOrderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LBOrderCell class]) forIndexPath:indexPath];[cell updateWithText:self.dataArray[indexPath.item]];return cell;
}#pragma mark - action- (void)longPressRecognizer:(UILongPressGestureRecognizer *)gestureRecognizer
{CGPoint point = [gestureRecognizer locationInView:self.collectionView];switch (gestureRecognizer.state) {case UIGestureRecognizerStateBegan: {NSIndexPath *selectedIndexPath = [self.collectionView indexPathForItemAtPoint:point];if (selectedIndexPath) {[self.collectionView beginInteractiveMovementForItemAtIndexPath:selectedIndexPath];}break;}case UIGestureRecognizerStateChanged: {[self.collectionView updateInteractiveMovementTargetPosition:point];break;;}case UIGestureRecognizerStateEnded: {[self.collectionView endInteractiveMovement];break;}default:{[self.collectionView cancelInteractiveMovement];}break;}
}#pragma mark - lazy load- (UICollectionView *)collectionView
{if (!_collectionView) {UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;CGFloat w = (CGRectGetWidth(self.view.bounds) - 2 * 8 - 2 * 20.5)/3;CGFloat space = 8;layout.itemSize = CGSizeMake(w, w);layout.minimumLineSpacing = space;layout.sectionInset = UIEdgeInsetsMake(0, 16, 0, 16);self.view.clipsToBounds = YES;_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, CGRectGetWidth(self.view.bounds), 350) collectionViewLayout:layout];[_collectionView registerClass:[LBOrderCell class] forCellWithReuseIdentifier:NSStringFromClass([LBOrderCell class])];_collectionView.dataSource = self;_collectionView.delegate = self;_collectionView.backgroundColor = [UIColor cyanColor];}return _collectionView;
}- (UILongPressGestureRecognizer *)longPressRecognizer
{if (!_longPressRecognizer) {_longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressRecognizer:)];_longPressRecognizer.minimumPressDuration = 0.3;}return _longPressRecognizer;
}- (NSMutableArray *)dataArray
{if (!_dataArray) {NSArray *array = @[@"1", @"2", @"3", @"4", @"5", @"6",@"7", @"8", @"9", @"10", @"11", @"12"];_dataArray = [NSMutableArray array];[_dataArray addObjectsFromArray:array];}return _dataArray;
}@end

链接: link
如果对您有帮助,请给一个star

这篇关于iOS调整collectionViewCell顺序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1049091

相关文章

Java中JSON格式反序列化为Map且保证存取顺序一致的问题

《Java中JSON格式反序列化为Map且保证存取顺序一致的问题》:本文主要介绍Java中JSON格式反序列化为Map且保证存取顺序一致的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未... 目录背景问题解决方法总结背景做项目涉及两个微服务之间传数据时,需要提供方将Map类型的数据序列化为co

MySQL中SQL的执行顺序详解

《MySQL中SQL的执行顺序详解》:本文主要介绍MySQL中SQL的执行顺序,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql中SQL的执行顺序SQL执行顺序MySQL的执行顺序SELECT语句定义SELECT语句执行顺序总结MySQL中SQL的执行顺序

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

SpringBoot中配置文件的加载顺序解读

《SpringBoot中配置文件的加载顺序解读》:本文主要介绍SpringBoot中配置文件的加载顺序,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot配置文件的加载顺序1、命令⾏参数2、Java系统属性3、操作系统环境变量5、项目【外部】的ap

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio

Python批量调整Word文档中的字体、段落间距及格式

《Python批量调整Word文档中的字体、段落间距及格式》这篇文章主要为大家详细介绍了如何使用Python的docx库来批量处理Word文档,包括设置首行缩进、字体、字号、行间距、段落对齐方式等,需... 目录关键代码一级标题设置  正文设置完整代码运行结果最近关于批处理格式的问题我查了很多资料,但是都没

Python中顺序结构和循环结构示例代码

《Python中顺序结构和循环结构示例代码》:本文主要介绍Python中的条件语句和循环语句,条件语句用于根据条件执行不同的代码块,循环语句用于重复执行一段代码,文章还详细说明了range函数的使... 目录一、条件语句(1)条件语句的定义(2)条件语句的语法(a)单分支 if(b)双分支 if-else(

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明

C++实现封装的顺序表的操作与实践

《C++实现封装的顺序表的操作与实践》在程序设计中,顺序表是一种常见的线性数据结构,通常用于存储具有固定顺序的元素,与链表不同,顺序表中的元素是连续存储的,因此访问速度较快,但插入和删除操作的效率可能... 目录一、顺序表的基本概念二、顺序表类的设计1. 顺序表类的成员变量2. 构造函数和析构函数三、顺序表

JAVA利用顺序表实现“杨辉三角”的思路及代码示例

《JAVA利用顺序表实现“杨辉三角”的思路及代码示例》杨辉三角形是中国古代数学的杰出研究成果之一,是我国北宋数学家贾宪于1050年首先发现并使用的,:本文主要介绍JAVA利用顺序表实现杨辉三角的思... 目录一:“杨辉三角”题目链接二:题解代码:三:题解思路:总结一:“杨辉三角”题目链接题目链接:点击这里