我只是想做的是用来自预先存在的 [String:String] 字典的数据填充我的表视图.我找到了无数关于如何使用数组执行此操作的示例和教程,但没有找到关于如何使用字典执行此操作的示例和教程.字典值以什么顺序放入 tableview 对我来说无关紧要.
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.
我有一个单独的 UITableViewCell 类,它有到我的单元格的出口,我只需要找出如何将这个字典链接到表视图.非常感谢任何和所有帮助,谢谢.
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.
推荐答案因为你永远不能保证你的字典内容会按照它进入的顺序出现,你不能直接从字典中完成.因此,您需要使用基于字典键的单独数组(例如)间接执行此操作.
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(<)然后在您的 UITableView 委托/数据源方法中使用 indexPath.row 作为 sortedKeys 数组的索引来获取数组的键.然后使用该键从字典中获取值.
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.
更多推荐
用字典中的数据填充 UITableView
发布评论