首页 文章

从字典数组Swift中填充UITableView

提问于
浏览
-1

我正在尝试从字典数组中填充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
}

1 回答

  • 0

    我相信类型转换是你在cellForRowAtIndexPath中遇到的问题 . 从数组中获取字典时,请包含以下代码:

    //假设myPosts数组中的字典是[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
    }
    

    谢谢

相关问题