collectionView--編輯全選刪除

weixin_33912246發表於2016-11-17
//
//  ViewController.m
//  collectionViewDemo
//
//  Created by qianfeng on 16/11/16.
//  Copyright © 2016年 qianfeng. All rights reserved.
//

#import "ViewController.h"
#import "Cell.h"
#import "Item.h"

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    BOOL isBtnEditing;
    BOOL isBtnSelected;
}
@property(strong,nonatomic) UICollectionView *collectionView;
@property(strong,nonatomic) NSMutableArray *dataSource;
@property(strong,nonatomic) UIButton *editBtn;
@property(strong,nonatomic) UIButton *selectBtn;


@end

@implementation ViewController

-(NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    isBtnEditing = NO;
    isBtnSelected = NO;
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 1;
    layout.minimumInteritemSpacing = 1;
    layout.itemSize = CGSizeMake(100, 150);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    //註冊
    [self.collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"Cell"];
    
    
    
    //編輯
    _editBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [_editBtn addTarget:self action:@selector(editBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _editBtn.frame = CGRectMake(0, 0, 80, 30);
    [_editBtn setTitle:@"編輯" forState:UIControlStateNormal];
    [_editBtn setTitle:@"取消編輯" forState:UIControlStateSelected];
    UIBarButtonItem *rightEditItem =  [[UIBarButtonItem alloc]initWithCustomView:_editBtn];
    //全選
    _selectBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [_selectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _selectBtn.frame = CGRectMake(0, 0, 80, 30);
    [_selectBtn setTitle:@"全選" forState:UIControlStateNormal];
    [_selectBtn setTitle:@"取消全選" forState:UIControlStateSelected];
    UIBarButtonItem *rightSelectItem =  [[UIBarButtonItem alloc]initWithCustomView:_selectBtn];
    self.navigationItem.rightBarButtonItems = @[rightEditItem,rightSelectItem];
    //刪除
    UIBarButtonItem *deleteBtn =  [[UIBarButtonItem alloc]initWithTitle:@"刪除" style:UIBarButtonItemStylePlain target:self action:@selector(deleteBtnClick:)];
    self.navigationItem.leftBarButtonItem = deleteBtn;
    
    
    
    for (NSInteger i = 0; i < 30; i ++) {
        
        Item *item = [[Item alloc]init];
        
        [self.dataSource addObject:item];
    }
}



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    
    Item *item = self.dataSource[indexPath.item];
    
    cell.item = item;
    
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"點選了第幾個:%li",indexPath.item + 1);
    
    Item *item = self.dataSource[indexPath.item];
    
    item.isSelected = YES;
    
    [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
}





-(void)editBtnClick:(UIButton *)sender
{
    isBtnEditing = !isBtnEditing;
    if (isBtnEditing) {
        for (NSInteger i = 0; i < self.dataSource.count; i ++) {
            Item *item = self.dataSource[i];
            item.isEditing = YES;
        }
    }else{
        for (NSInteger i = 0; i < self.dataSource.count; i ++) {
            Item *item = self.dataSource[i];
            item.isEditing = NO;
        }
    }
    [self.collectionView reloadData];
}


-(void)selectBtnClick:(UIButton *)sender
{
    isBtnSelected = !isBtnSelected;
    if (isBtnSelected) {
        for (NSInteger i = 0; i < self.dataSource.count; i ++) {
            Item *item = self.dataSource[i];
            item.isSelected = YES;
        }
    }else{
        for (NSInteger i = 0; i < self.dataSource.count; i ++) {
            Item *item = self.dataSource[i];
            item.isSelected = NO;
        }
    }
    [self.collectionView reloadData];
}



-(void)deleteBtnClick:(UIButton *)sender
{
    NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
    
        for (NSInteger i = 0; i < self.dataSource.count; i ++) {
            
            Item *item = self.dataSource[i];
            
            if (item.isSelected == YES) {
                
                [indexSet addIndex:i];
                
                NSLog(@"刪除第幾個:%li",i);
            }
        }
    [self.dataSource removeObjectsAtIndexes:indexSet];
    
    [self.collectionView reloadData];
}

@end

demo下載

相關文章