Swift:通过索引访问字典

标签 swift uitableview dictionary indexing

我想在我的 UITableViewCells 中使用字典中的值。我可以通过使用 indexPath.row 作为值并使用 indexPath.section 作为键从字典中获取它们吗?

最佳答案

你可以获取键数组并在 indexPath.section 中获取键,如下所示:

Array(yourDictionary.keys)[indexPath.section]

您可以像这样从值数组中获取 indexPath.row 中的值:

Array(yourDictionary.values)[indexPath.row]

编辑:

如果你想获取特定部分键的值,你应该这样写:

let key = Array(yourDictionary.keys)[indexPath.section]
let array = yourDictionary[key]
let value = array[indexPath.row]

关于Swift:通过索引访问字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31775724/

相关文章:

ios - 离散余弦变换的不同结果和代码......这是怎么回事?

python - 来自嵌套字典的 Pandas 数据框

Java 8 : Change the values of an EntrySet stream

swift - 无法将类型 'Data' 的值转换为指定类型 'Data'

iOS( swift ): UITextField is **not** first responder after becomeFirstResponder method is called?

ios - 动态单元格大小uitableview

ios - 自定义单元格到 ScrollView 与使用表格 View

python - configparser 中的多维字典

ios - 重新加载 UIAlertController 数据

ios - 重新加载包含 UIPageViewController 的 DetailViewController