ios - 在 Swift 4 中使用字典填充 UITableView 数据

标签 ios swift xcode dictionary tableview

我目前正在尝试使用字典中的数据填充 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/

相关文章:

iOS UINavigationBar 向下移动 View

ios - 从 Swift 2.3 (xCode 7) 迁移 --> Swift 4(xCode 9) + 更新 Cocoapods

ios - 如何让我的节点以较慢的持续时间旋转?

ios - 快速警报操作处理程序无法正常工作特定情况

iOS Security.framework arm64架构问题

ios - 重复符号 FB Audience Network iOS

ios - 如何在 iOS 上获取 Facebook 归因 ID?

ios - trigger.io 不会启动 IOS 模拟器

ios - 在 iOS 上使用 AVAudioPlayer 播放静音的最佳方式

ios - 如何删除tableview、coredata、firebase数据库中的数据,删除顺序?