dwin812
dwin812

Reputation: 133

how to display global dictionary items in table view

There are two views in my project: in the first one users input two words and those words are saved in a dictionary as the key and the value. The code in short:

var words = [Dictionary<String, String>]()//<-declared outside the class
words.append([input.text: input2.text] as! [String : String])

Then in the second view, I want to display all the saved words in table cell like so: table view

And I have found some code for doing that, but it works only for a local dict:

class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


// Data model: These strings will be the data for the table view cells
let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]

// cell reuse id (cells that scroll out of view can be reused)
let cellReuseIdentifier = "cell"

// don't forget to hook this up from the storyboard
@IBOutlet var tableView: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()

    // Register the table view cell class and its reuse id
    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)

    // (optional) include this line if you want to remove the extra empty cell divider lines
    // self.tableView.tableFooterView = UIView()

    // This view controller itself will provide the delegate methods and row data for the table view.
    tableView.delegate = self
    tableView.dataSource = self
}

// number of rows in table view
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return self.animals.count
}

// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // create a new cell if needed or reuse an old one
    let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!

    // set the text from the data model
   cell.textLabel?.text = self.animals[indexPath.row]

    return cell
}

// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("You tapped cell number \(indexPath.row).")
}
}

I have edited the code above like so:

var wordIndex1 = words.count - 1

class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var word = words[wordIndex1] as? Dictionary<String, String>

    let cellReuseIdentifier = "cell"

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)

        tableView.delegate = self
        tableView.dataSource = self
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (self.word?.count)!
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!

        cell.textLabel?.text = self.word?.keys.first

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("You tapped cell number \(indexPath.row).")
    }
}

But it only displays the last added value whereas in the first code it creates a new cell for each new value.

What did I do wrong?

Upvotes: 1

Views: 170

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100533

You access array index = number of items you need

var wordIndex1 = words.count - 1

or in short

if let lastWord =  words.last {
  ////
}

note array indexing starts with 0 and to numberOfElemnts - 1


Also it's better to have a model

struct Item {
  let leftInp.rightInp:String
}

then create the array like

var arr = [Item]()

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return words.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!

    let dic = words[indexPath.row]

    cell.textLabel?.text = dic.keys.first

    return cell
}

Upvotes: 1

Related Questions