I am trying to implement a shift sequence, but problems have come out. When added to @IBAction - self.isEditing = true, nothing happens when a button is pressed. On the Internet, I found an entry self.tableView.setEditing (true, animated: true), connected with View. When you click on the button, the table editing opens, but the button is not returned to the save state, so the edited table is not saved. What is the problem?

@IBAction func editButton(_ sender: UIBarButtonItem) { //self.isEditing = true self.tableView.setEditing(true, animated: true) } func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) { } tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { return true } 

enter image description here

    2 answers 2

    In the method, you need to implement the replacement of objects from your sourceArray, from which the table receives data, on top of each other. Sorry about Objective-C, but I think the point is clear.

     - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { NSMutableArray *sourceArrayCopy = [_sourceArray mutableCopy]; [sourceArrayCopy replaceObjectAtIndex:sourceIndexPath withObject:sourceArray[destinationIndexPath]]; [sourceArrayCopy replaceObjectAtIndex:destinationIndexPath withObject:sourceArray[sourceIndexPath]]; _sourceArray = sourceArrayCopy; 

    }

      If on Swift, then you can use the following code:

       override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { let item = YourItemsArray[sourceIndexPath.row] YourItemsArray.remove(at: sourceIndexPath.row) YourItemsArray.insert(item, at: destinationIndexPath.row) } 
      • Added, but changing the table does not turn off and the button name does not change - Artur Skachkov
      • See how this button is implemented. For example, you can specify in the constructor: navigationItem.rightBarButtonItem = editButtonItem , where var editButtonItem: UIBarButtonItem {get} is a UIViewController property. - Pavel Osipenko