在 TableView 中迭代字典
这可能是一种常见的情况,但我无法找出如何做到这一点的方法。
我有一个字典,其中包含多个商品的库存量: dict = ["item1" : 2, "item2" : 5, "item3" : 10 ]
我现在如何将键和值分配给tableView 中的不同标签,使用 indexPath
?
我想要实现的逻辑:
cell.itemLabel.text = dict.[firstItemName]
cell.amountLabel.text = dict.[firstItemAmount]
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用字典作为数据源可能会很烦人,因为字典是无序的。
我的建议是将字典
映射
到自定义结构的数组,并按键对数组进行排序然后在表视图中您可以编写
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
您可以使用映射函数来获取单独的键和值数组
试试这个:
You can use map function to get separate array of key and values
Try this: