【问题标题】:how to get the array values stored in variable and show them in tableview?如何获取存储在变量中的数组值并在表格视图中显示它们?
【发布时间】:2020-11-25 11:07:09
【问题描述】:

我在使用 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 中显示这些数组?

【问题讨论】:

  • 你能分享我的api吗,我会为你做完整的演示。
  • 您的请求代码是什么?你的尝试是什么?不相关,但在 Swift 3+ 中,尽可能喜欢 Stuff 而不是 NSStuff,所以 NSArray => Array.另外,为什么不使用 Codable?

标签: swift xcode alamofire


【解决方案1】:

请试试这个代码

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
  }
}

【讨论】:

    【解决方案2】:

    我认为这段代码对你有用

          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
           arrayData.count
         }
    
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "ContactTableCell", for: indexPath) as! ContactTableCell
        
         var userId = arrayData[indexPath.row].user_id
         }
    

    【讨论】:

      【解决方案3】:

      你可以使用

      let decoder = JSONDecoder()
      decoder.keyEncodingStrategy = .convertToSnakeCase
      do {
         let res = try decoder.decode(Root.self,from:data)
      } catch { 
         print(error)
      }
      

      struct Root: Codable {
          let emergencyContacts: [EmergencyContact] 
      }
      
      // MARK: - EmergencyContact
      struct EmergencyContact: Codable {
          let id, userId: Int
          let firstName, lastName, email, phoneNumber: String
          let createdAt, updatedAt: String
          let deletedAt: String? 
      }
      

      【讨论】:

        猜你喜欢
        • 2019-02-02
        • 1970-01-01
        • 2013-12-14
        • 1970-01-01
        • 2015-08-05
        • 1970-01-01
        • 1970-01-01
        • 2014-06-13
        • 2017-08-19
        相关资源
        最近更新 更多