//
// 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
tableview--编辑全选删除
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 开发过程中或多或少都会遇到tableview的各种功能,这里简单记录一下tableview的删除和全选删除功能,废...
- 在以往的惯性思维中,对于批量删除的操作一直以为是标记被选中的Cell 控件进行删除,但往往前面被选中的Cell会被...
- 我们主动找对方说话的一方,往往叫做沟通的主动方,主动发起的一场沟通,我们就叫做“沟通邀请”。 任何希望和对方能够建...