//
// 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
collectionView--编辑全选删除
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 01.想到爸爸,没有亲昵的感觉,更多的是拘谨和尴尬 对于大部分女孩,撒娇似乎是一件信手拈来的事情。 而我,作为资深...