【问题标题】:not getting table view data after reloading tableview重新加载表格视图后未获取表格视图数据
【发布时间】:2019-06-10 12:55:07
【问题描述】:

我正在使用AlamofireSwiftyJSOn 调用api,我正在创建模型类以在tableview 中显示数据,这是我的回应

回应

{
  "inspections_todays_data" : [

  ],
  "message" : "Successfully.",
  "success" : "1",
  "inspections_future_data" : [

  ],
  "inspections_overdue_data" : [
    {
      "notes" : "Test",
      "surveyor_id" : "8",
      "longitude" : null,
      "time" : "08:00",
      "address3" : null,
      "county" : "carlow",
      "client_id" : "3",
      "house_num" : "test street",
      "eircode" : "12345",
      "address1" : "test area",
      "date_inspected" : "2019-01-10",
      "address2" : "test city",
      "country" : "Ireland",
      "latitude" : null,
      "name" : "test street",
      "property_id" : "22"
    }
  ]
}

正如您在响应中看到的那样,我有三个数组,我正在获取 inspections_overdue_data 数组,我已成功获取但无法在 tableview 上显示

这是我的代码

代码

  func OverdueList(){
        let preferences = UserDefaults.standard
        let uid = "u_id"
        let acTkn = "acc_tkn"

        let u_ID = preferences.object(forKey: uid)
        let A_Token = preferences.object(forKey: acTkn)

        let params = ["user_id": u_ID!, "access_token": A_Token!]
        print(params)
        Alamofire.request(inspectionsList, method: .post, parameters: params).responseJSON(completionHandler: {(response) in
            switch response.result{
            case.success(let value):
                let json  = JSON(value)
                print(json)
                let data = json["inspections_overdue_data"]
                print(data)
                if data == []{
                    self.viewNodata.isHidden = false
                }else{
                    data.array?.forEach({ (iunOverDue) in
                        let iOveList = OvedueModel(surveyor_id: iunOverDue["surveyor_id"].stringValue, country: iunOverDue["country"].stringValue, time: iunOverDue["time"].stringValue, address2: iunOverDue["address2"].stringValue, notes: iunOverDue["notes"].stringValue, house_num: iunOverDue["house_num"].stringValue, name: iunOverDue["name"].stringValue, address1: iunOverDue["address1"].stringValue, eircode: iunOverDue["eircode"].stringValue, date_inspected: iunOverDue["date_inspected"].stringValue, property_id: iunOverDue["property_id"].stringValue, county: iunOverDue["county"].stringValue, client_id: iunOverDue["client_id"].stringValue)
                        print(iOveList)
                        self.overDueData.append(iOveList)
                    })

                    self.tblOvedue.reloadData()
                }

            case.failure(let error):
                print(error.localizedDescription)
            }

        })
    }

我还通过故事板添加了委托和数据源,但我仍然无法在此处显示数据,这是我的 tableview 方法

   func numberOfSections(in tableView: UITableView) -> Int {
        return 0
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return overDueData.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! OverDueTableViewCell
        let name = overDueData[indexPath.row].name
        let address1 = overDueData[indexPath.row].address1
        cell.lblTitle.text = "\(name) \(address1)"
        return cell
    }

【问题讨论】:

    标签: ios swift uitableview alamofire swifty-json


    【解决方案1】:

    numberOfSections 返回 0,如果您想查看任何内容,您必须至少返回 1。

    提示:如果你只有 1 个部分,你可以省略它,因为 1 是默认返回值。

    【讨论】:

    • 哦,是的,它起作用了,对不起,我犯了愚蠢的错误,谢谢
    • @VishalParmar 你应该接受这个答案,它首先得到了正确的回答
    【解决方案2】:

    您应该删除以下 tableview 的委托方法,或者您应该返回至少 1 个。

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    

    【讨论】:

    • 这是对以下内容的复制
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多