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:
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
CodePudding user response:
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()
}
}