//
// 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下载
网友评论