Update Model on row move

I’ve got a table with 3 sections and the model I’m using is defined as playingList : [[Player]]. In my situation

playingList[0] = players are playing
playingList[1] = players are substitutes
playingList[2] = players are injured/unavailable

I’ve managed to move my uitableviewcells through sections, but struggling to update the model, the method I thought I needed doesn’t seem to be called. These are the methods I have around this, I obviously don’t quite understand them or am missing something

    // delegates for re-ordering
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle
{
    if (false == self.editing){
        return UITableViewCellEditingStyle.None;
    }
    
    if (self.editing && indexPath.row == orderedPlayers.count){
        return UITableViewCellEditingStyle.Insert;
    }
    else {
        return UITableViewCellEditingStyle.Delete;
    }
}

// Update the data model according to edit actions delete or insert.
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
    /*if editingStyle == UITableViewCellEditingStyle.Delete{
        orderedPlayers.removeAtIndex(indexPath.row);
        playerTableView.reloadData();
    }*/
    // arbitrarily define two indexPaths for testing purposes
    let fromIndexPath = NSIndexPath(forRow: 0, inSection: 0)
    let toIndexPath = NSIndexPath(forRow: 0, inSection: 1)
    
    // swap the data between the 2 (internal) arrays
    let dataPiece = playingList[fromIndexPath.section][fromIndexPath.row]
    playingList[toIndexPath.section].insert(dataPiece, atIndex: toIndexPath.row)
    playingList[fromIndexPath.section].removeAtIndex(fromIndexPath.row)
    // Do the move between the table view rows
    playerTableView.moveRowAtIndexPath(fromIndexPath, toIndexPath: toIndexPath)
}

The above method doesn’t seem to be called as I tried to print details from this method to the console. What do I need to do to update my data Model to accurately reflect where an item is in an array of arrays?

Have you updated to Swift 3 by any chance? I’ve had a lot of trouble with functions not being recognised due to either the missing underscore in IBAction, or changed function signatures. Because commitEditingStyle is one of the optional functions you won’t get an error…

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)

is the correct (for today anyway!)

Hi @aeberbach

I haven’t taken on Swift three yet, but I do have the answer to my question. This was my final code:

    // Update the data model according to edit actions delete or insert.
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
    if editingStyle == UITableViewCellEditingStyle.Delete{
        playingList[indexPath.section].removeAtIndex(indexPath.row)
    }
}

    // Process the row move. This means updating the data model to correct the item indices.
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)
{
    let moveFrom = NSIndexPath(forRow: sourceIndexPath.row, inSection: sourceIndexPath.section)
    let moveTo = NSIndexPath(forRow: destinationIndexPath.row, inSection: destinationIndexPath.section)
    
    // swap the data between the 2 arrays
    let dataPiece = playingList[moveFrom.section][moveFrom.row]
    playingList[moveTo.section].insert(dataPiece, atIndex: moveTo.row)
    playingList[moveFrom.section].removeAtIndex(moveFrom.row)
}

Looks good. Any ideas on why the commitEditingStyle function wasn’t being called?

Not really, I think I’d probably not understood how the function worked and assigned it within the wrong if statement.