Home > Software engineering >  Array of Arrays not properly working with emojis
Array of Arrays not properly working with emojis

Time:03-31

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