p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #000000; background-color: rgba(0, 0, 0, 0) }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #000000; background-color: rgba(0, 0, 0, 0); min-height: 20.0px }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 14.0px "PingFang SC"; color: #000000; background-color: rgba(0, 0, 0, 0) }
p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #000000 }
span.s1 { }
span.s2 { background-color: rgba(0, 0, 0, 0) }
第一种方式(普通):
// 定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
// 进入编辑模式,按下出现的编辑按钮后,进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//获取模型
SubDevice* subDevModel=self.subDeviceArray[indexPath.row];
if (editingStyle == UITableViewCellEditingStyleDelete) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确定删除?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//请求接口,删除数据
//[self loadForDeleteTimingTaskData];
//删除数据,并刷新列表
[self.subDeviceArray removeObject:subDevModel];
[self.tableView reloadData];
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了取消");
}];
[alert addAction:action1];
[alert addAction:action2];
[self presentViewController:alert animated:YES completion:nil];
}
}
//添加编辑模式
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
// 修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"删除";
}
//设置进入编辑状态时,Cell不会缩进
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
return NO;
}
第二种方式(iOS8 API):
//让tableView可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
//添加一个删除按钮
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//先删数据 再删UI
[self.subDeviceArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadData];
}];
//添加一个置顶按钮
UITableViewRowAction *topAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
SubDevice* subDevModel=self.subDeviceArray[indexPath.row];
[self.subDeviceArray removeObjectAtIndex:indexPath.row];
[self.subDeviceArray insertObject:subDevModel atIndex:0];
[tableView moveRowAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
[tableView setEditing:NO];
[tableView reloadData];
}];
topAction.backgroundColor = [UIColor blueColor];
//添加一个编辑按钮
UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"修改" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
SubDevice* subDevModel=self.subDeviceArray[indexPath.row];
//弹窗输入名字
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"修改" message:@"请输入新名字" preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"此处输入名字";
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.borderStyle = UITextBorderStyleRoundedRect;
}];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[tableView setEditing:NO];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UITextField *textfield = alert.textFields.firstObject;
NSString *newName = textfield.text;
if (newName == nil) {
newName = @"";
}
subDevModel.name = newName;
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadData];
}]];
[self presentViewController:alert animated:YES completion:nil];
}];
editAction.backgroundColor = [UIColor greenColor];
return @[deleteAction, topAction, editAction];
}
第三种方式(iOS11 API):
//iOS11后的新方法,使用tableView左滑删除cell
-(UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath{
//获取模型
SubDevice* subDevModel=self.subDeviceArray[indexPath.row];
//删除
if (@available(iOS 11.0, *)) {
UIContextualAction* deleteRowAction=[UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确定删除?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//请求接口,删除数据
//[self loadForDeleteTimingTaskData];
//删除数据,并刷新列表
[self.subDeviceArray removeObject:subDevModel];
[self.tableView reloadData];
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了取消");
}];
[alert addAction:action1];
[alert addAction:action2];
[self presentViewController:alert animated:YES completion:nil];
}];
UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
return config;
} else {
// Fallback on earlier versions
return nil;
}
}
原文地址:https://www.cnblogs.com/yuhao309/p/9497385.html