在一些测试中,我被一些我不太了解的东西卡住了,看起来表格单元的生成是在页面加载和Alamofire请求之前发生的,而不是在页面加载之后发生的。
如果您在下面看到以下内容,请尝试在咨询专家后将其转到查看我们博物馆的出站货件的位置:
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")]
所以我知道数据已经适当地传递给了模型。我还将注意到情节提要中的“表视图单元格标识符”已正确设置为ShipmentCell
。但是查询之后,我的表中没有任何内容弹出。
我正在使用Swift 4。
答案 0 :(得分:0)
更新源字段后,您应该重新加载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")
}
}
}