1.前言
近日碰到了UITableView的多選刪除操作,雖然不是太復(fù)雜,但是還是做個記錄吧,以后再遇到可以直接使用了,同時也希望能對有需要的人帶來幫助。
2.效果展示
包含功能有:全選,全不選,多個刪除,單個左滑刪除

未命名.gif
3.代碼及注釋
#import "SelectAndDeleteVC.h"
@interface SelectAndDeleteVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
//底部刪除View
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIButton *selectButton;
@end
@implementation SelectAndDeleteVC
- (void)viewDidLoad {
[super viewDidLoad];
_dataArray = [[NSMutableArray alloc] initWithArray:@[@"測試數(shù)據(jù)1",@"測試數(shù)據(jù)2",@"測試數(shù)據(jù)3"]];
[self drawView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)drawView{
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"編輯" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemAction:)];
self.navigationItem.rightBarButtonItem = item;
_tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
//在編輯時可以多選,開啟后就顯示圓形選擇框
_tableView.allowsMultipleSelectionDuringEditing = YES;
_tableView.tableFooterView = [[UIView alloc] init];
[self.view addSubview:_tableView];
[self.view addSubview:self.bottomView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = _dataArray[indexPath.row];
return cell;
}
- (UIView *)bottomView{
if (!_bottomView){
_bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight , ScreenWidth, 50*UIRate)];
_bottomView.backgroundColor = [UIColor whiteColor];
UIButton *selectButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth/2.0, 50*UIRate)];
[selectButton setTitle:@"全選" forState:UIControlStateNormal];
[selectButton setBackgroundColor:[UIColor grayColor]];
[selectButton addTarget:self action:@selector(selectButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[_bottomView addSubview:selectButton];
UIButton *deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth/2.0, 0, ScreenWidth/2.0, 50*UIRate)];
[deleteButton setTitle:@"刪除" forState:UIControlStateNormal];
[deleteButton setBackgroundColor:[UIColor redColor]];
[deleteButton addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[_bottomView addSubview:deleteButton];
}
return _bottomView;
}
//走了左滑刪除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
DLog(@"走了左滑刪除%ld", (long)indexPath.row);
if (editingStyle == UITableViewCellEditingStyleDelete){//刪除操作
[self.dataArray removeObjectAtIndex:indexPath.row];
//刪除某行并配有動畫
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
//更改左滑后的字體顯示
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
//如果系統(tǒng)是英文,會顯示delete,這里可以改成自己想顯示的內(nèi)容
return @"刪除";
}
//全選
- (void)selectButtonAction:(UIButton *)button{
if ([button.currentTitle isEqualToString:@"全選"]){
//遍歷循環(huán)
[self.dataArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//全選
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
}];
[button setTitle:@"全不選" forState:UIControlStateNormal];
}else {
//刷新數(shù)據(jù)即可實現(xiàn)全不選狀態(tài)
[self.tableView reloadData];
[button setTitle:@"全選" forState:UIControlStateNormal];
}
}
//刪除
- (void)deleteBtnAction:(UIButton *)button{
//indexPathsForSelectedRows 可以獲得選中的行
NSArray *array = self.tableView.indexPathsForSelectedRows;
if (!array.count){
[LCProgressHUD showMessage:@"請選擇要刪除的列表"];
return;
}
NSMutableArray *selectArray = [[NSMutableArray alloc] init];
for (NSIndexPath *indexPath in array){
//取出數(shù)據(jù)
[selectArray addObject:self.dataArray[indexPath.row]];
}
//刪除數(shù)據(jù)
[self.dataArray removeObjectsInArray:selectArray];
//刪除行
[self.tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];
}
#pragma mark 右編輯
- (void)rightItemAction:(UIBarButtonItem *)item
{
if (!self.tableView.isEditing){
item.title = @"取消";
//伴隨編輯動畫
[self.tableView setEditing:YES animated:YES];
[UIView animateWithDuration:0.5 animations:^{
self.bottomView.frame = CGRectMake(0, ScreenHeight - 50, ScreenWidth, 50);
}];
}else {
item.title = @"編輯";
[self.tableView setEditing:NO animated:YES];
[UIView animateWithDuration:0.5 animations:^{
self.bottomView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 50);
}];
}
}
@end