this might be a common situation, but I was not able to figure out a way how to do it.
I have a dictionary with the stock amount for several items: dict = ["item1" : 2, "item2" : 5, "item3" : 10 ]
How can I now assign the keys and values to different labels in a tableView, using indexPath
?
The logic I want to achieve:
cell.itemLabel.text = dict.[firstItemName]
cell.amountLabel.text = dict.[firstItemAmount]
2
Answers
You can use map function to get separate array of key and values
Try this:
A dictionary as data source can be annoying because a dictionary is unordered.
My suggestion is to
map
the dictionary to an array of a custom struct and sort the array by the keysThen in the table view you can write