第一種方式(普通):spa
// 定義編輯樣式接口
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {ip
return UITableViewCellEditingStyleDelete;rem
}it
// 進入編輯模式,按下出現的編輯按鈕後,進行刪除操做io
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {table
//獲取模型class
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;
}
}