will cohen
will cohen

Reputation: 75

Populating a UITableView with data from a dictionary

what I am simply trying to do is populate my table view with data from a preexisting [String:String] dictionary. I have found countless examples and tutorials on how to do this with an array, but none on how to do it with a dictionary. It does not matter to me in what order the dictionary values get put in the tableview.

I have a separate UITableViewCell class that has outlets to my cell, and I just need to find out how to link this dictionary to the table view. Any and all help is greatly appreciated, thank you.

Upvotes: 2

Views: 1007

Answers (1)

Son of a Beach
Son of a Beach

Reputation: 1779

Because you can never guarantee that your dictionary contents will come out in the same order it went in, you cannot do it directly from a dictionary. Therefore you would need to do it indirectly using a separate array that is based on the dictionary keys (for example).

One such way of doing this would be to create an array based on the sorted keys of the dictionary thusly:

sortedKeys = Array(dictionary.keys).sorted(<)

Then in your UITableView delegate/data-source methods use indexPath.row as index to the sortedKeys array to grab a key the array. Then use that key, to grab the value from your dictionary.

Upvotes: 1

Related Questions