-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle) forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle==UITableViewCellEditingStyleDelete) {
// 获取选中删除行索引值
NSInteger row = [indexPath row];
// 通过获取的索引值删除数组中的值
[self.listData removeObjectAtIndex:row];
// 删除单元格的某一行时,在用动画效果实现删除过程
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
#pragma mark -- cell左滑动退出编辑
//设cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
// [tableView setEditing:YES animated:YES];
return UITableViewCellEditingStyleDelete;
}
//修改编辑按钮的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"退出";
}
//进入编辑模式,点击出现的编辑按钮后
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"你确定退出该编辑?" preferredStyle:UIAlertControllerStyleAlert];
// [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//
[_classArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}]];
[self presentViewController:alertController animated:YES completion:nil];
}
}