Swipe To Delete TableView Row

后端 未结 8 939
暖寄归人
暖寄归人 2020-12-24 06:10

I have my array:

self.colorNames = [[NSArray alloc] 
initWithObjects:@\"Red\", @\"Green\",
@\"Blue\", @\"Indigo\", @\"Violet\", nil];

I\'ve

相关标签:
8条回答
  • 2020-12-24 06:52

    You have to implement the necessary UITableViewDelegate and UITableViewDataSource methods.

    First, add this:

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
        return YES;
    }
    

    Then:

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            //remove the deleted object from your data source.
            //If your data source is an NSMutableArray, do this
            [self.dataArray removeObjectAtIndex:indexPath.row];
            [tableView reloadData]; // tell table to refresh now
        }
    }
    
    0 讨论(0)
  • 2020-12-24 06:53

    Swift 3 and Swift 4 answer without using reloadData

    I prefer using deleteRows instead of using reloadData.

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Enables editing only for the selected table view, if you have multiple table views
        return tableView == yourTableView
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Deleting new item in the table
            yourTableView.beginUpdates()
            yourDataArray.remove(at: indexPath.row)
            yourTableView.deleteRows(at: [indexPath], with: .automatic)
            yourTableView.endUpdates()
        }
    }
    
    0 讨论(0)
  • 2020-12-24 06:56

    You have to add UITableViewDelegate and UITableViewDataSource methods.

    You can add multiple buttons on UITableView Row Swipe:

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
            return YES;
        }
    
    -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
        
            UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
            {
                // Delete something here
            }];
            delete.backgroundColor = [UIColor redColor];
        
            UITableViewRowAction *more = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@" More " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
            {
               //You function call:
            }];
            more.backgroundColor = [UIColor colorWithRed:0.188 green:0.514 blue:0.984 alpha:1];
        
            return @[delete, more]; //array with all the buttons.
        }`
    
    0 讨论(0)
  • 2020-12-24 07:00

    Swift Version:

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
         if editingStyle == UITableViewCellEditingStyle.Delete {
             dataArray?.removeAtIndex(indexPath.row)
             tableview.reloadData()
         }
    }
    
    0 讨论(0)
  • 2020-12-24 07:02

    Implement the following method:

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return UITableViewCellEditingStyleDelete;
    }
    
    0 讨论(0)
  • 2020-12-24 07:04

    For a start, your colorNames should be an NSMutableArray rather than an NSArray. You can’t add or remove objects from a regular (non-mutable) array; you’d have to recreate it each time you made a change. Switching that will make this easier. For your implementation of -tableView:commitEditingStyle:forRowAtIndexPath:, you’ll then be able to do something like this:

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if(editingStyle == UITableViewCellEditingStyleDelete)
        {
            [colorNames removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        }
    }
    
    0 讨论(0)
提交回复
热议问题