gpt4 book ai didi

ios - UITableView 和模型数组中的单元格

转载 作者:行者123 更新时间:2023-11-29 05:47:45 24 4
gpt4 key购买 nike

我陷入了一些我不太明白的事情,因为从一些测试来看,表格单元格的生成似乎是在页面加载和 Alamofire 请求之前发生的,但在页面加载和 Alamofire 请求之后则不然。

如果您看到下面的内容,我正在尝试在引用专业人士后将其转到查看我们博物馆的出境 cargo 的位置:

import Alamofire
import SwiftyJSON

class ShipmentProSearchResultsTableViewController: UITableViewController {

var pronumber:String = ""
var shipments = [Shipment]()
typealias JSONStandard = [String: AnyObject]

override func viewDidLoad() {
super.viewDidLoad()
fetchShipments()

}

func fetchShipments() {
let parameters: Parameters = ["pro_number": pronumber]
let todoEndpoint: String = "OURHOST/shipments/api/details/pro"
Alamofire.request(todoEndpoint, method: .get, parameters: parameters)
.responseJSON { response in
if response.result.isSuccess{
let shipmentJSON : JSON = JSON(response.result.value!)
for (index, subJson):(String, JSON) in shipmentJSON{
let proNumber = subJson["proNumber"].int
let consigneeName = subJson["consignee"]["name"].string
let shipment = Shipment(proNumber: proNumber!, consigneeName: consigneeName!)

self.shipments.append(shipment)
}
print(self.shipments)
}else{
print("Could not get results")
}
}
}


// MARK: - Table view data source

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return shipments.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ShipmentCell", for: indexPath)
let shipment = shipments[indexPath.row]

cell.textLabel?.text = "Hello"
cell.detailTextLabel?.text = "\(shipment.proNumber)"

return cell
}
}

现在,我打印了 self.shipments,得到了以下结果:

[OakRidgeArchaeologicalRepositoryDispatcher.Shipment(proNumber: 471008276, consigneeName: "A1 CHICAGO INSTITUTE OF THE ARTS")]

所以我知道数据已正确传递给模型。我还将注意到 Storyboard中的 TableView 单元标识符已正确设置为 ShipmentCell。但查询后,我的表中没有弹出任何内容。

我使用的是 Swift 4。

最佳答案

更新源字段后,您应该重新加载tableView

func fetchShipments() {
let parameters: Parameters = ["pro_number": pronumber]
let todoEndpoint: String = "OURHOST/shipments/api/details/pro"
Alamofire.request(todoEndpoint, method: .get, parameters: parameters)
.responseJSON { response in
if response.result.isSuccess{
let shipmentJSON : JSON = JSON(response.result.value!)
for (index, subJson):(String, JSON) in shipmentJSON{
let proNumber = subJson["proNumber"].int
let consigneeName = subJson["consignee"]["name"].string
let shipment = Shipment(proNumber: proNumber!, consigneeName: consigneeName!)

self.shipments.append(shipment)
}
tableView.reloadData() //<-------add this.
print(self.shipments)
}else{
print("Could not get results")
}
}
}

关于ios - UITableView 和模型数组中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55980432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com