iOS之实现按钮拖动/tableViewcell多选,拖动/collectionviewcell多选,拖动(交换位置)
来源:互联网 发布:mac导入铃声 编辑:程序博客网 时间:2024/05/22 15:04
1.===============通过tableview:实现
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tab;
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,strong)NSMutableArray *selectArr;//选中的添加到数组
@end
static NSString *idenfier=@"cell";
@implementation ViewController
-(NSMutableArray *)selectArr{
if(_selectArr==nil){
_selectArr =[[NSMutableArrayalloc]init];
}
return_selectArr;
}
-(NSMutableArray *)dataArr{
if(_dataArr==nil){
_dataArr=[NSMutableArrayarrayWithObjects:@"one",@"two",@"three",@"four",@"five",@"six",nil];
}
return_dataArr;
}
- (void)viewDidLoad {
[superviewDidLoad];
UITableView *tab=[[UITableViewalloc]initWithFrame:self.view.framestyle:UITableViewStylePlain];
tab.dataSource=self;
tab.delegate=self;
self.tab=tab;
[self.viewaddSubview:tab];
[self.tabregisterClass:[UITableViewCellclass]forCellReuseIdentifier:idenfier];
tab.allowsMultipleSelection=YES;
tab.editing=YES;
}
//必须把编辑模式改成None,默认的是delete
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
returnUITableViewCellEditingStyleNone;
}
#pragma mark 排序当移动了某一行时候会调用
//编辑状态下,只要实现这个方法,就能实现拖动排序---右侧会出现三条杠,点击三条杠就能拖动
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 取出要拖动的模型数据
id goods = self.dataArr[sourceIndexPath.row];
//删除之前行的数据
[self.dataArrremoveObject:goods];
// 插入数据到新的位置
[self.dataArrinsertObject:goodsatIndex:destinationIndexPath.row];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
returnself.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier:idenfier];
cell.textLabel.text=self.dataArr[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[tableViewcellForRowAtIndexPath:indexPath];
cell.contentView.backgroundColor=[UIColorredColor];
[self.selectArraddObject:self.dataArr[indexPath.row]];
NSLog(@"添加后的数组-----%@",self.selectArr);
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[tableViewcellForRowAtIndexPath:indexPath];
cell.contentView.backgroundColor=[UIColorblueColor];
if(self.selectArr.count >indexPath.row){
[self.selectArrremoveObjectAtIndex:indexPath.row];
}
NSLog(@"取消后的数组----%@",self.selectArr);
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
2.==============通过collection View实现:
#import "ViewController.h"
#import "Headerview.h"
#import "FooterView.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView *collec;
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,strong)NSMutableArray *selectArr;
@end
static NSString *identifier=@"cell";
@implementation ViewController
-(NSMutableArray *)dataArr{
if(_dataArr==nil){
_dataArr=[NSMutableArrayarrayWithObjects:@"one",@"two",@"three",@"four",@"five",@"six",@"seven",@"eight",@"nine",nil];
}
return_dataArr;
}
-(NSMutableArray *)selectArr{
if(_selectArr==nil){
_selectArr=[[NSMutableArrayalloc]init];
}
return_selectArr;
}
- (void)viewDidLoad {
[superviewDidLoad];
UICollectionViewFlowLayout *flow=[[UICollectionViewFlowLayoutalloc]init];
flow.itemSize=CGSizeMake(50,50);
flow.sectionInset=UIEdgeInsetsMake(20,20,20,20);
self.collec=[[UICollectionViewalloc]initWithFrame:self.view.framecollectionViewLayout:flow];
[self.collecregisterClass:[UICollectionViewCellclass]forCellWithReuseIdentifier:identifier];
self.collec.allowsMultipleSelection=YES;//允许多选
self.collec.delegate=self;
self.collec.dataSource=self;
[self.viewaddSubview:self.collec];
self.collec.backgroundColor=[UIColorwhiteColor];
//注册组头
[self.collecregisterClass:[Headerviewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
//注册组尾
[self.collecregisterClass:[FooterViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
//此处给其增加长按手势,用此手势触发cell移动效果
UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(handlelongGesture:)];
[self.collecaddGestureRecognizer:longGesture];
}
- (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture {
//判断手势状态
switch (longGesture.state) {
caseUIGestureRecognizerStateBegan:{
//判断手势落点位置是否在路径上
NSIndexPath *indexPath = [self.collecindexPathForItemAtPoint:[longGesturelocationInView:self.collec]];
if (indexPath ==nil) {
break;
}
//在路径上则开始移动该路径上的cell
[self.collecbeginInteractiveMovementForItemAtIndexPath:indexPath];
}
break;
caseUIGestureRecognizerStateChanged:
//移动过程当中随时更新cell位置
[self.collecupdateInteractiveMovementTargetPosition:[longGesturelocationInView:self.collec]];
break;
caseUIGestureRecognizerStateEnded:
//移动结束后关闭cell移动
[self.collecendInteractiveMovement];
break;
default:
[self.colleccancelInteractiveMovement];
break;
}
}
//让cell可以移动-----交换cell的位置
-(BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"canmove");
returnYES;
}
//哪一个cell移动到哪一位置----把数据源中的位置也交换
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath{
//取出源item数据
id objc = [self.dataArrobjectAtIndex:sourceIndexPath.item];
//从资源数组中移除该数据
[self.dataArrremoveObject:objc];
//将数据插入到资源数组中的目标位置上
[self.dataArrinsertObject:objcatIndex:destinationIndexPath.item];
NSLog(@"moveto");
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
returnself.dataArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell=[collectionViewdequeueReusableCellWithReuseIdentifier:identifierforIndexPath:indexPath];
UILabel *lbl=[[UILabelalloc]initWithFrame:CGRectMake(0,0,60,30)];
lbl.text=self.dataArr[indexPath.item];
[cell.contentViewaddSubview:lbl];
cell.backgroundColor=[UIColorredColor];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell=[collectionViewcellForItemAtIndexPath:indexPath];
cell.backgroundColor=[UIColorgreenColor];
[self.selectArraddObject:self.dataArr[indexPath.row]];
NSLog(@"选择前----%@",self.selectArr);
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell=[collectionViewcellForItemAtIndexPath:indexPath];
cell.backgroundColor=[UIColoryellowColor];
if(self.selectArr.count >indexPath.row){
[self.selectArrremoveObjectAtIndex:indexPath.row];
}
NSLog(@"选择后---%@",self.selectArr);
}
//====================
//选择cell的时候会调用
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"shouldselect");
returnYES;
}
//取消选择cell的时候会调用
-(BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"shoulddeselect");
returnYES;
}
-(void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{
}
//==========设置组头和组尾的时候必须返回组头和组尾的view和设置她们的size
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
UICollectionReusableView *resuableView;
if(kind ==UICollectionElementKindSectionHeader){//组头
resuableView = [collectionView dequeueReusableSupplementaryViewOfKind:kindwithReuseIdentifier:@"header"forIndexPath:indexPath];
}else{//组尾
resuableView = [collectionView dequeueReusableSupplementaryViewOfKind:kindwithReuseIdentifier:@"footer" forIndexPath:indexPath];
}
return resuableView;
}
//设置组头view的size
-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
if(section ==0)
{
CGSize size = {50,50};
return size;
} else
{
CGSize size = {50,50};
return size;
}
}
//设置组尾view的size
-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
if(section ==0)
{
CGSize size = {50,50};
return size;
} else
{
CGSize size = {50,50};
return size;
}
}
//===========================================
-(void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{
}
@end
- iOS之实现按钮拖动/tableViewcell多选,拖动/collectionviewcell多选,拖动(交换位置)
- iOS collectionViewCell拖动动画
- 【NGUI】扩展NGUI例子实现拖动交换位置功能
- Android使用ItemTouchHelper实现RecyclerView的item拖动位置交换
- 【NGUI】扩展NGUI例子实现拖动交换位置功能
- 拖动
- 拖动
- 拖动
- 拖动
- 可以拖动交换item位置的GridView
- 可以拖动交换item位置的GridView
- jQuery-UI 拖动Div交换位置
- UICollectionView的cell拖动交换位置
- jquery拖动兼交换位置插件
- c#实现按钮拖动效果
- android实现可拖动按钮
- IOS UITableView 多选,全选删除,拖动,置顶的实现
- iOS 实现拖动一个view
- Petri网可达分析算法
- SIFT学习记录
- pip install urllib2不能安装
- 桥接模式下socket回应
- 文章标题
- iOS之实现按钮拖动/tableViewcell多选,拖动/collectionviewcell多选,拖动(交换位置)
- Android辅助功能AccessibilityService与抢红包辅助
- 二维码的生成,扫描(ZXing开源的依赖库)
- 关于mysql和orcal相关问题
- 关于 C:\Windows\System32\drivers\etc\下面的 hosts文件
- 【LeetCode】 203. Remove Linked List Elements C语言
- 《PCL点云库学习&VS2010(X64)》Part 28 BoundingBox&addArray
- Nginx反向代理实现Tomcat集群服务器的负载均衡
- 温习HTML(五)