【问题标题】:Populate UITableView from Array of Dictionaries Swift从字典数组 Swift 中填充 UITableView
【发布时间】:2017-11-04 12:12:46
【问题描述】:

我正在尝试从字典数组中填充 UITableVIew。这是我的 tableView / cellForRowAt 方法。 myPosts 是数组,里面有字典。当我使用 [indexPath.row] 时,它会将类型更改为“Any”并且不能将其转换为 dict。 我的问题是如何使用 indexPath 访问字典键的值?

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell

    let activePosts = myPosts[indexPath.row]
    print(activePosts)
    cell.customCellUsername.text = myUser
    cell.customCellPost.text = 
    cell.customCellImage.image = 
    cell.customCellUserImage.image =
    return cell
}

【问题讨论】:

    标签: ios arrays swift uitableview dictionary


    【解决方案1】:

    我相信类型转换是您在 cellForRowAtIndexPath 中面临的问题。从数组中获取字典时,包含以下代码:

    //假设myPosts Array中的字典是[String:String]类型

    if let postDictionary = myPosts[indexPath.row] as? [String:String] {
      print(postDictionary) //Will print the dictionary in the array for 
                            //  index as equal to indexpath.row
    }
    

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-21
      • 2017-11-24
      • 1970-01-01
      相关资源
      最近更新 更多