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;



}


//设置组头viewsize

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

    }


}

//设置组尾viewsize

-(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


3.===============
0 0
原创粉丝点击