Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

tableView return function not getting the data from API Call instead its making the tableView empty

I made API file with the request data in Json, also made a different structure of the type data that will come but I am having problems retrieving the data, its showing empty tableView instead of showing at least the cells I made.

This is the return code for tableView:

extension MonitorimiViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return vehicles.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "monitorimiCell", for: indexPath) as! MyCustomCell1
    cell.configure()
    return cell   
}

This is the structure file with the data that must be filled in the Cell labels:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

struct Vehicles: Codable {

var  Plate: String?
var  Speed: Int
var  LastCommunicationDate: Int
var  Passengers: Int
var  Driver: String

}

And this is the API request file:

class APICaller {

static let shared = APICaller()
private let baseURL = "http://000.000.000.000:3030/api/"

private init() {}

func getVehicles(for id: String, IMEI: Int, completed: @escaping (Result<[Vehicles],OnTraErrors>) -> Void ){
    let endpoint = baseURL + "GetVehicle/?UserIdentificationValue=JJGI243GJI3G&IMEI=623624346236"
    
    let headers = [
                "content-type": "application/json",
                "authorizetoken": "NjQzOPA2N0NDNDFAH4CNDk3R23F2FQUY0NjV3FFE=",
                "cache-control": "no-cache",
                ]
    
    guard let url = URL(string: endpoint) else {
        completed(.failure(.invalidURL))
        return
    }
    
    var request = URLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 120)
            request.httpMethod = "POST"
            request.allHTTPHeaderFields = headers
    
    let session = URLSession.shared
    let task = session.dataTask(with: request) { data, response, error in
        
        if let _ = error {
            completed(.failure(.unableToComplete))
            return
        }
        guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
            completed(.failure(.invalidResponse))
            return
        }
        guard let data = data else {
            completed(.failure(.invalidData))
            return
        }
        do {
            let decoder = JSONDecoder()
            decoder.keyDecodingStrategy = .convertFromSnakeCase
            let vehicles = try decoder.decode([Vehicles].self, from: data)
            completed(.success(vehicles))
        } catch {
            completed(.failure(.invalidData))
        }
    }
    task.resume()
}

So when I change the return vehicles.count to return 10 it just shows my cells like this : [1]: https://i.stack.imgur.com/aVp5Z.png

but right now its showing me this instead of filling those labels [2]: https://i.stack.imgur.com/RG4bb.png

>Solution :

make sure you’re reloading your UITableView you can simply observe the dataSource variable

    var vehicles: [Vehicles] = [] { // i suggest renaming Vehicles struct to vehicle
        didSet {
                tableView.reloadDate()
        }
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading