Home > Mobile >  Rearranging Cells in an Array of Arrays Crash and not updating correctly in Swift
Rearranging Cells in an Array of Arrays Crash and not updating correctly in Swift

Time:04-01

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: "           
  • Related