Vishal Parmar
Vishal Parmar

Reputation: 613

not getting table view data after reloading tableview

i am call api using Alamofire and SwiftyJSOn and i am creating model class for display data in tableview here is my response

Response

{
  "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"
    }
  ]
}

as you see in response i have three array and i am fetching inspections_overdue_data array i fetched successfully but not able to display on tableview

Here is my code

Code

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

        })
    }

i also added delegate and datasource through storyboard but still i am not able to show data here is my tableview methods

   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
    }

Upvotes: 0

Views: 74

Answers (2)

Parth
Parth

Reputation: 626

You should either remove below tableview's delegate method or you should return at-least 1.

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

Upvotes: 1

Mattia C.
Mattia C.

Reputation: 731

numberOfSections is returning 0, you have to return at least 1 if you want to see anything.

Protip: If you only have 1 section, you can omit it since 1 is the default return value.

Upvotes: 1

Related Questions