tableview--编辑全选删除
2016-11-16 本文已影响42人
Hyman0819
//
// ViewController.m
// tableViewDemo
//
// Created by qianfeng on 16/11/16.
// Copyright © 2016年 qianfeng. All rights reserved.
//
#import "ViewController.h"
#import "Cell.h"
static NSString *cellID = @"Cell";
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
BOOL isEditBtnSelected;
BOOL isSelectBtnSelected;
}
@property(strong,nonatomic) UITableView *tableView;
@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];
isEditBtnSelected = NO;
isSelectBtnSelected = NO;
self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
self.tableView.allowsMultipleSelectionDuringEditing = YES;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.rowHeight = 60;
//注册
[self.tableView registerNib:[UINib nibWithNibName:cellID bundle:nil] forCellReuseIdentifier:cellID];
//编辑
_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;
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
return cell;
}
//编辑
-(void)editBtnClick:(id)sender
{
isEditBtnSelected = !isEditBtnSelected;
self.editBtn.selected = isEditBtnSelected;
if (isEditBtnSelected) {
self.tableView.editing = YES;
}else{
self.tableView.editing = NO;
}
}
//全选
-(void)selectBtnClick:(id)sender
{
isSelectBtnSelected = !isSelectBtnSelected;
self.selectBtn.selected = isSelectBtnSelected;
if (isSelectBtnSelected) {
for (NSInteger i = 0; i < self.dataSource.count; i ++) {
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:0];
[self.tableView selectRowAtIndexPath:indexpath animated:YES scrollPosition:0];
}
}else{
for (NSInteger i = 0; i < self.dataSource.count; i ++) {
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:0];
[self.tableView deselectRowAtIndexPath:indexpath animated:YES];
}
}
}
// 删除
-(void)deleteBtnClick:(id)sender
{
//获取所有
NSArray *indexpaths = [self.tableView indexPathsForSelectedRows];
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
for (NSIndexPath *indexpath in indexpaths) {
[indexSet addIndex:indexpath.row];
}
[self.dataSource removeObjectsAtIndexes:indexSet];
[self.tableView deleteRowsAtIndexPaths:indexpaths withRowAnimation:UITableViewRowAnimationBottom];
}
@end