我目前正在尝试使用字典中的数据填充 UITableView。我收到错误消息“无法下标‘字典’类型的值”。我知道这意味着什么,但我不知道如何解决它。我知道如何让它与数组一起工作。可以用字典吗?
// my dictionary
var contactDictionary: Dictionary = [String: String]()
//function that throws error
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "contactcell", for: indexPath) as! UITableViewCell
let object = contactDictionary[indexPath.row]
}
最佳答案
您可以使用 contactDictionary.keys 检索键数组并使用键查找值,但顺序可能每次都不同; Swift 不保证字典的顺序。如果顺序很重要,您可以搜索 3rd 方 OrderedDictionary 或其他数据结构。
关于ios - 在 Swift 4 中使用字典填充 UITableView 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51776899/