這就是在模擬器中運行應用程式看起來只是空白而不是用來自 API 的資料填充標簽:(
這就是它應該的樣子,ofc 右側的標簽已填充,但是當我輸入代碼以使用從 api 到標簽的資料填充單元格時,它什么也沒顯示?
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
}
}
這是我將 var 中的 5 個呼叫到我的單元格的結構:
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?
}
uj5u.com熱心網友回復:
您需要為從 IBOutlet 連接的 tableview 呼叫 reloadData,而不是為 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
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/363658.html
