- 左滑刪除
//聲明
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;//數(shù)據(jù)源
//懶加載
- (UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
}
return _tableView;
}
- (NSMutableArray *)dataArray
{
if (!_dataArray) {
_dataArray = [NSMutableArray array];
}
return _dataArray;
}
//添加tableView
- (void)createUI
{
[self.view addSubview:self.tableView];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.and.trailing.equalTo(@0);
make.top.equalTo(self.mas_topLayoutGuide);
make.bottom.equalTo(self.mas_bottomLayoutGuide);
}];
}
//數(shù)據(jù)
- (void)loadData
{
for (int i = 0; i < 20; i ++) {
[self.dataArray addObject:[NSString stringWithFormat:@"%d", i]];
}
}
//數(shù)據(jù)源代理 -展示數(shù)據(jù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *iden = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
}
cell.textLabel.text = self.dataArray[indexPath.row];
return cell;
}
//刪除代理
//是否可編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.dataArray removeObjectAtIndex:indexPath.row];//刪除數(shù)據(jù)源中相應數(shù)據(jù)
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
//修改刪除操作title -默認 "刪除"(Delete) -跟隨系統(tǒng)語言
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"左滑刪除";
}
左滑刪除.gif
- 編輯狀態(tài)下刪除
與左滑返回區(qū)別: 設置編輯狀態(tài)為YES
self.tableView.editing = !self.tableView.editing;
//編輯風格為刪除
if (self.editingStyle == CCTableViewCellEditingStyleDelete) {
return UITableViewCellEditingStyleDelete;
}
點擊編輯逊移,出現(xiàn)減號预吆,再點擊減號 出現(xiàn)左滑返回按鈕 點擊左滑返回按鈕 刪除數(shù)據(jù)
編輯刪除.gif
- 編輯狀態(tài)下插入
//編輯風格為插入
if (self.editingStyle == CCTableViewCellEditingStyleInsert) {
return UITableViewCellEditingStyleInsert;
}
//插入操作
[self.dataArray insertObject:@"cc" atIndex:indexPath.row];//插入數(shù)據(jù)
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
編輯插入.gif
- 編輯風格為刪除和插入共存
if (self.editingStyle == (CCTableViewCellEditingStyleDelete | CCTableViewCellEditingStyleInsert)) {
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
//聲明
@property (nonatomic, strong) NSMutableDictionary *deleteDic;//記錄多選刪除數(shù)據(jù)
@property (nonatomic, strong) UIBarButtonItem *deleteBtn;
//顯示刪除按鈕
if (self.editingStyle == (CCTableViewCellEditingStyleInsert | CCTableViewCellEditingStyleDelete)) {
[self.deleteDic removeAllObjects];
self.deleteBtn = [[UIBarButtonItem alloc] initWithTitle:@"刪除" style:UIBarButtonItemStylePlain target:self action:@selector(deleteBtnAction)];
NSMutableArray *Items = [[NSMutableArray alloc]initWithObjects:self.deleteBtn, nil];
if (self.tableView.editing) {
[self.navigationController setToolbarHidden:NO animated:YES];
}else {
[self.navigationController setToolbarHidden:YES animated:YES];
}
[self setToolbarItems:Items];
self.deleteBtn.enabled = NO;
}
//點擊cell選擇刪除行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//indexPath為key 對應數(shù)據(jù)為值
[self.deleteDic setObject:_dataArray[indexPath.row] forKey:indexPath];
if (self.deleteDic.count == 0) {
self.deleteBtn.enabled = NO;
} else {
self.deleteBtn.enabled = YES;
}
}
//取消行
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.deleteDic removeObjectForKey:indexPath];
if (self.deleteDic.count == 0) {
self.deleteBtn.enabled = NO;
} else {
self.deleteBtn.enabled = YES;
}
}
//刪除操作
//多選刪除操作
[_dataArray removeObjectsInArray:[self.deleteDic allValues]];
[self.tableView deleteRowsAtIndexPaths:[self.deleteDic allKeys] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.deleteDic removeAllObjects];
[self.tableView setEditing:!self.tableView.editing animated:YES];
[self.navigationController setToolbarHidden:YES animated:YES];
多選刪除.gif
左滑刪除很多應用中都會此功能淤齐,而其他的系統(tǒng)自帶功能或許并不一定符合實際需求矛缨;關于多選刪除,過幾天會分享一個小例子