class MonitorimiViewController: UIViewController {
@IBOutlet weak var monitorimiTableView: UITableView!
var listOfVechicle = [Vehicles]()
let tableView = UITableView()
var id = "6438367CC43848B497FE4604AF465DSSS6A"
override func viewDidLoad() {
super.viewDidLoad()
monitorimiTableView.delegate = self
monitorimiTableView.dataSource = self
monitorimiTableView.rowHeight = 225
APICaller.shared.getVehicles(for: id) {[weak self] (result) in
guard let self = self else { return }
switch result {
case .success(let vehicle):
self.listOfVechicle = vehicle
DispatchQueue.main.async {
self.tableView.reloadData()
}
case .failure(let error):
print(error)
extension MonitorimiViewController: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listOfVechicle.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? MyCustomCell1 else {return UITableViewCell()}
//You can Access Your Vehicles Model Values Here one by one
let currentVechicle = listOfVechicle[indexPath.row]
cell.veturaOutlet.text = currentVechicle.Plate
cell.shpejtsiaAktualeOutlet.text = currentVechicle.Speed
cell.perditsuarOutlet.text = currentVechicle.LastCommunicationDate
return cell
}
}
And this is the structure which I am calling 5 of the var to my cells:
struct Vehicles: Codable {
var IDVehicle: Int?
var Title: String?
var RegistrationDate: String?
var ExpireDate: String?
var Department: String?
var Identification: String?
var Speed: String?
var Latitude: Double?
var Longitude: Double?
var Angle: Int?
var Status: Int?
var InputValue: Int?
var Plate: String?
var LastCommunicationDate: String?
var Passengers: Int?
var Driver: String?
}
CodePudding user response:
you need to call reloadData for the tableview which is connected from IBOutlet not for Dummy TableView
class MonitorimiViewController: UIViewController {
@IBOutlet weak var monitorimiTableView: UITableView!
var listOfVechicle = [Vehicles]()
//No Need For the tableView Creation ,So pls remove that,beacuse you had create and connected the tableview through storyboard(IBoutlet)
//let tableView = UITableView()
var id = "6438367CC43848B497FE4604AF465DSSS6A"
override func viewDidLoad() {
super.viewDidLoad()
monitorimiTableView.delegate = self
monitorimiTableView.dataSource = self
monitorimiTableView.rowHeight = 225
fetchAndReloadData()
}
func fetchAndReloadData(){
APICaller.shared.getVehicles(for: id) {[weak self] (result) in
guard let self = self else { return }
switch result {
case .success(let vehicle):
self.listOfVechicle = vehicle
DispatchQueue.main.async {
//Instead of below code
// self.tableView.reloadData()
// use like Below
self.monitorimiTableView.reloadData()
}
case .failure(let error):
print(error)
}
}
}
}
extension MonitorimiViewController: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listOfVechicle.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? MyCustomCell1 else {return UITableViewCell()}
//You can Access Your Vehicles Model Values Here one by one
let currentVechicle = listOfVechicle[indexPath.row]
cell.veturaOutlet.text = currentVechicle.Plate
cell.shpejtsiaAktualeOutlet.text = currentVechicle.Speed
cell.perditsuarOutlet.text = currentVechicle.LastCommunicationDate
return cell
}
}