//
// 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];
//注冊(cè)
[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(@"點(diǎn)擊了第幾個(gè):%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(@"刪除第幾個(gè):%li",i);
}
}
[self.dataSource removeObjectsAtIndexes:indexSet];
[self.collectionView reloadData];
}
@end
collectionView--編輯全選刪除
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 體驗(yàn)入:剛剛收到媽媽的信息,問我最近干嘛了,是缺錢么,細(xì)問之下才知道她收到一條詐騙短信,內(nèi)容是我貸款了,沒有還上。...
- 01.想到爸爸,沒有親昵的感覺,更多的是拘謹(jǐn)和尷尬 對(duì)于大部分女孩,撒嬌似乎是一件信手拈來的事情。 而我,作為資深...