闲来无事写的小demo 实现侧滑删除 全选删除
先上属性
@property (nonatomic ,strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;//数据
@property(nonatomic, strong) NSMutableArray *deleteArr;//删除数据的数组
@property (nonatomic, strong) UIView *funcView;//底部全选 移除 视图
@property (nonatomic, strong) UIView * noDataView;//没有数据视图
创建tableView
- (UITableView *)tableView {
if (_tableView == nil) {
_tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableView.backgroundColor = [UIColor whiteColor];
// [_tableView registerClass:[HXMMyCollectionCell class] forCellReuseIdentifier:cellId];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.rowHeight = 50;
// _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.editing = NO;
}
return _tableView;
}
数据源数组
- (NSMutableArray *)dataArray {
if (_dataArray == nil) {
_dataArray = [NSMutableArray arrayWithArray:@[@"科比·布莱恩特",@"德里克·罗斯",@"勒布朗·詹姆斯",@"凯文·杜兰特",@"德怀恩·韦德",@"克里斯·保罗",@"德怀特·霍华德",@"德克·诺维斯基",@"德隆·威廉姆斯",@"斯蒂夫·纳什",@"保罗·加索尔",@"布兰顿·罗伊",@"奈特·阿奇博尔德",@"鲍勃·库西",@"埃尔文·约翰逊"]];
}
return _dataArray;
}
数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ident = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ident];
}
cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];
return cell;
}
侧滑删除
#pragma mark 滑动手势删除
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 添加一个删除按钮
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了删除");
// 1. 更新数据
// [_allDataArray removeObjectAtIndex:indexPath.row];
// 2. 更新UI
// [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
}];
// 将设置好的按钮放到数组中返回
return @[deleteRowAction];
}
多选代理设置
//是否可以编辑 默认的时YES
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//选择编辑的方式,按照选择的方式对表进行处理
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
}
}
//选择你要对表进行处理的方式 默认是删除方式
//-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
// return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
//}
//选中时将选中行的在self.dataArray 中的数据添加到删除数组self.deleteArr中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.deleteArr addObject:[self.dataArray objectAtIndex:indexPath.row]];
}
//取消选中时 将存放在self.deleteArr中的数据移除
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.deleteArr removeObject:[self.dataArray objectAtIndex:indexPath.row]];
}
基础页面
#pragma mark - 设置界面
- (void)setupUI {
self.view.backgroundColor = [UIColor whiteColor];
[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:19.0], NSForegroundColorAttributeName : [UIColor whiteColor]}];
[self.navigationController.navigationBar setTranslucent:NO];
[self.navigationController.navigationBar setTintColor:[UIColor redColor ]];
// [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:(UIColor lightGrayColor) size:CGSizeMake(Screen_width, Screen_height)] forBarMetrics:UIBarMetricsDefault];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑"
style:UIBarButtonItemStylePlain
target:self
action:@selector(selectedBtn:)];
[self.view addSubview:self.tableView];
UIView *funcView = [[UIView alloc] init];
_funcView = funcView;
funcView.hidden = YES;
funcView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:funcView];
[funcView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.bottom.right.equalTo(self.view);
make.height.mas_equalTo(50);
}];
UIButton * btn = [[UIButton alloc] init];
[btn setTitle:@"全选" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[funcView addSubview:btn];
[btn addTarget:self action:@selector(selectAllBtnClick:)
forControlEvents:UIControlEventTouchUpInside];
[btn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.bottom.equalTo(self.view);
make.height.mas_equalTo(49);
make.width.mas_equalTo(SCREEN_WIDTH/2);
}];
UIButton * delBtn = [[UIButton alloc] init];
[delBtn setTitle:@"移除" forState:UIControlStateNormal];
[delBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[funcView addSubview:delBtn];
[delBtn addTarget:self action:@selector(deleteClick:)
forControlEvents:UIControlEventTouchUpInside];
[delBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(btn.mas_right);
make.bottom.equalTo(self.view);
make.height.mas_equalTo(49);
make.width.mas_equalTo(SCREEN_WIDTH/2);
}];
}
对应按钮的方法
//选择按钮点击响应事件
- (void)selectedBtn:(UIButton *)button {
//支持同时选中多行
self.tableView.allowsMultipleSelectionDuringEditing = YES;
self.tableView.editing = !self.tableView.editing;
if (self.tableView.editing) {
self.navigationItem.rightBarButtonItem.title = @"取消";
_funcView.hidden = NO;
}else{
self.navigationItem.rightBarButtonItem.title = @"编辑";
_funcView.hidden = YES;
}
}
//全选
- (void)selectAllBtnClick:(UIButton *)button {
for (int i = 0; i < self.dataArray.count; i ++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
[self.deleteArr addObjectsFromArray:self.dataArray];
}
NSLog(@"self.deleteArr:%@", self.deleteArr);
}
//删除按钮点击事件
- (void)deleteClick:(UIButton *) button {
if (self.tableView.editing) {
//删除
[self.dataArray removeObjectsInArray:self.deleteArr];
[self.tableView reloadData];
if (self.dataArray.count == 0) {
self.navigationItem.rightBarButtonItem.title = @"编辑";
_funcView.hidden = YES;
}
}
else return;
}