'Rearranging Cells in an Array of Arrays Crash and not updating correctly in Swift

I have a program that sorts/displays emojis in different category's. This program has an edit button where the user is able to move an "entry"/cell then when they click done the program re-adjusts. The array(s) should update and so should the running totals at the bottom of each section.

The problem is that when the moving in complete, if a category now does not have a cell the program crash. The other problem is that when moving a cell, it pushes another cell to replace it.

Meaning if the category "People" has four cells and I move one cell another cell from a different category takes its place.

I believe the problem is in the chunk of code:

// Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
        let movingEmoji = emojis[fromIndexPath.section].remove(at: fromIndexPath.row)
        emojis[fromIndexPath.section].insert(movingEmoji, at: to.row)
    }

Fatal error: Array index is out of range

Program code for reference:

import UIKit

class EmojiTableViewController: UITableViewController {

    var emojis: [[Emoji]] = [
            
            [Emoji(symbol: "😀", name: "Grinning Face", description: "A typical smiley face.", usage: "happiness"),
            Emoji(symbol: "😕", name: "Confused Face", description: "A confused, puzzled face.", usage: "unsure what to think; displeasure"),
            Emoji(symbol: "😍", name: "Heart Eyes", description: "A smiley face with hearts for eyes.", usage: "love of something; attractive"),
            Emoji(symbol: "👮", name: "Police Officer", description: "A police officer wearing a blue cap with a gold badge. He is smiling, and eager to help.", usage: "person of authority")],
            
            
            [Emoji(symbol: "🐢", name: "Turtle", description: "A cute turtle.", usage: "Something slow"),
            Emoji(symbol: "🐘", name: "Elephant", description: "A gray elephant.", usage: "good memory")],
            
            
            [Emoji(symbol: "🍝", name: "Spaghetti", description: "A plate of spaghetti.", usage: "spaghetti")],
             
             
            [Emoji(symbol: "🎲", name: "Die", description: "A single die.", usage: "taking a risk, chance; game"),
            Emoji(symbol: "⛺️", name: "Tent", description: "A small tent.", usage: "camping"),
            Emoji(symbol: "📚", name: "Stack of Books", description: "Three colored books stacked on each other.", usage: "homework, studying"),
            Emoji(symbol: "💔", name: "Broken Heart", description: "A red, broken heart.", usage: "extreme sadness"),
            Emoji(symbol: "💤", name: "Snore", description: "Three blue \'z\'s.", usage: "tired, sleepiness"),
            Emoji(symbol: "🏁", name: "Checkered Flag", description: "A black and white checkered flag.", usage: "completion")]
        ]
    
    let myTitles: [String] = ["PEOPLE", "ANIMALS", "FOOD", "OTHER"]
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: 50))
            
            let label = UILabel()
            label.frame = CGRect.init(x: 5, y: 5, width: headerView.frame.width-10, height: headerView.frame.height-10)
            label.text = myTitles[section]
            label.font = .systemFont(ofSize: 14)
            label.textColor = .gray
            headerView.addSubview(label)
            
            return headerView
        }
        
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return 40
        }
    
    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
            let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: 50))
            
            let label = UILabel()
            label.frame = CGRect.init(x: 5, y: 5, width: headerView.frame.width-10, height: headerView.frame.height-10)
            label.text = ("Total Emojis: \(emojis[section].count)")
            label.font = .systemFont(ofSize: 12)
            label.textColor = .gray
            headerView.addSubview(label)
            
            return headerView
        }
        
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
            return 40
        }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

    @IBAction func refreshData(_ sender: UIRefreshControl) {
        tableView.reloadData()
        sender.endRefreshing()
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return emojis.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return emojis[section].count

    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "emojiCell", for: indexPath)

            let emoji = emojis[indexPath.section][indexPath.row]
            cell.textLabel?.text = "\(emoji.symbol) - \(emoji.name)"
            cell.detailTextLabel?.text = emoji.description

            return cell
        }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("You clicked at \(indexPath)")
    }
    
    override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
        print("You clicked emoji \(emojis[indexPath.row])")
    }


    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            emojis[indexPath.section].remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        }
    }

    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
        let movingEmoji = emojis[fromIndexPath.section].remove(at: fromIndexPath.row)
        emojis[fromIndexPath.section].insert(movingEmoji, at: to.row)
    }

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
        if let vc = segue.destination as? EmojiDetailViewController {
            if let indexPath = tableView.indexPathForSelectedRow {
                vc.emoji = emojis[indexPath.section][indexPath.row]
            }
        }
    }
    

}


Solution 1:[1]

You are right, the issue is with the lines of code in tableView moveRowAt to

The issue is you are inserting the moved data into the wrong array / section.

This is what you have now:

// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView,
                        moveRowAt fromIndexPath: IndexPath,
                        to: IndexPath) {
    
    // This is ok
    let movingEmoji = emojis[fromIndexPath.section].remove(at: fromIndexPath.row)
    
    // This is not, the You want to insert this into the to.section
    // not fromIndexPath
    emojis[fromIndexPath.section].insert(movingEmoji, at: to.row)
}

Change this to

// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView,
                        moveRowAt fromIndexPath: IndexPath,
                        to: IndexPath) {
    
    // This is ok
    let movingEmoji = emojis[fromIndexPath.section].remove(at: fromIndexPath.row)
    
    // Use the to section
    emojis[to.section].insert(movingEmoji, at: to.row)
}

Give this a go, I think it should work now.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1