我正在尝试从字典数组中填充UITableVIew。这是我的tableView/cellForRowAt 方法。myPosts是包含字典的数组。当我使用 [indexPath.row] 时,它将类型更改为"任何",并且无法在字典中强制转换它。我的问题是如何使用 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
}
我相信
类型转换是您在cellForRowAtIndexPath中面临的问题。从数组获取字典时,请包含以下代码:
假设 myPosts 数组中的字典是 [字符串:字符串] 类型
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
}
谢谢