swift - 如何获取存储在变量中的数组值并在表格视图中显示它们?
问题描述
我在使用 Alamofire 的 Post 请求的 JSON 响应中有这个数组
"emergency_contacts": [
{
"id": 8,
"user_id": 11,
"first_name": "abc",
"last_name": "abc",
"email": "svsg@hh.ggg",
"phone_number": "+27676800080",
"created_at": "2020-07-30T23:09:10.000000Z",
"updated_at": "2020-07-30T23:09:10.000000Z",
"deleted_at": null
}
{
"id": 9,
"user_id": 11,
"first_name": "xxc",
"last_name": "xxc",
"email": "sh2.ggg",
"phone_number": "+27676800080",
"created_at": "2020-07-30T23:09:10.000000Z",
"updated_at": "2020-07-30T23:09:10.000000Z",
"deleted_at": null
}
],
我已经保存了这样的回复
let arrayData = userData["emergency_contacts"] as! NSArray
我如何在某些 var 中有这些值并在 tableView 中显示这些数组?
解决方案
请尝试此代码
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell.init(style: .default, reuseIdentifier: "cell")
let dicData = arrayData.object(at: indexPath.row) as? NSDictionary
print(dicData?.value(forKey: "id") as Any)
print(dicData?.value(forKey: "user_id") as Any)
print(dicData?.value(forKey: "first_name") as Any)
print(dicData?.value(forKey: "last_name") as Any)
print(dicData?.value(forKey: "email") as Any)
print(dicData?.value(forKey: "phone_number") as Any)
print(dicData?.value(forKey: "created_at") as Any)
print(dicData?.value(forKey: "updated_at") as Any)
print(dicData?.value(forKey: "deleted_at") as Any)
return cell
}
}
推荐阅读
- grep - 如何在匹配的行中grep仍然未知的特定单词
- python - 铁路预订系统 - 保留可用座位数
- css - 如何在 VueJS 中平滑过渡动画
- android - 如何为 AVD 管理器创建自定义 Android 设备配置文件?
- javascript - 从 asp.net 页面调用 JS 函数
- flutter - 控制器的长度属性与 TabBar 的选项卡属性中的选项卡数量不匹配
- java - On ButtonClick 各种表格单元格的背景色
- java - 在等待 lambda 失败条件下处理内部异常
- javascript - 无法读取 null 的属性“提交”我使用 laravel 和 javascript
- java - DSpace - SolrCore 初始化失败,无法创建目录