'Array of Arrays not properly working with emojis

I have created an array of arrays for emojis and separated the emojis into their proper grouping.

Step 1: Your array is an array of array of Emojis. So the type is not [Emoji] anymore. Its more like [[Emoji]]. Which means you can get access to the third grouping with a double index, such as:

let groupThirdCount = emojis[2].count

Note that emojis.count will no longer return the number of emojis, but rather than number of arrays of emojis.

Step 2: Update the datasource methods numberOfSections and numberOfRowsInSection to return the number of emojis based on which section is asked for.

Edited Code:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //changed from 0 to 3
    if section == 3 {
        return emojis.count
    } else {
        //changed from 0 to 3
        return 3
    }
}

And:

override func numberOfSections(in tableView: UITableView) -> Int {
    //changed from 0 to 3
    return 3
}

Step 3: Update the cellForRowAt method to get the emoji, not the array of emojis, also based on section.

Edited code:

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

    // Configure the cell...
    //added [[]] to emojis this is throwing 2 errors
    let emoji = [[emojis]][indexPath.row] 
    cell.textLabel?.text = "\(emoji.symbol) - \(emoji.name)"
    cell.detailTextLabel?.text = emoji.description
    
    cell.showsReorderControl = true

    return cell
}

Question: Can someone check my edits to the code and point me in the right direction if something is wrong?

Full Code:

import UIKit

class EmojiTableViewController: UITableViewController {

    var emojis: [Emoji] = [
        // People Array
        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"),
        
        // Animals
        Emoji(symbol: "🐢", name: "Turtle", description: "A cute turtle.", usage: "Something slow"),
        Emoji(symbol: "🐘", name: "Elephant", description: "A gray elephant.", usage: "good memory"),
        
        // Other
        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")
    ]
    
    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 {
        //changed from 0 to 3
        return 3
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //changed from 0 to 3
        if section == 3 {
            return emojis.count
        } else {
            //changed from 0 to 3
            return 3
        }
    }

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

        // Configure the cell...
        let emoji = [[emojis]][indexPath.row]
        //added [[]] to emojis
        cell.textLabel?.text = "\(emoji.symbol) - \(emoji.name)"
        cell.detailTextLabel?.text = emoji.description
        
        cell.showsReorderControl = true

        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.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.remove(at: fromIndexPath.row)
        emojis.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.row]
            }
        }
    }
    

}


Solution 1:[1]

   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")]
    ]
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return emojis[section].count

}
override func numberOfSections(in tableView: UITableView) -> Int {
    return emojis.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
    }

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 udi