gpt4 book ai didi

swift - 为什么我的数据没有显示在表格 View 中?

转载 作者:行者123 更新时间:2023-11-28 13:13:41 25 4
gpt4 key购买 nike

在控制台中打印数据效果很好。在 viewdidload 方法中,数组计数也很好。但是,如果我在 viewdidload 之外使用任何代码,它就无法正常工作。我试图用每个单元格(航空公司和价格)中的两个标签填充一个表,但没有显示任何数据,当我在控制台 arrayOfFlights.price.count 中尝试(例如)时,它打印出 0,就好像它是空的一样。

数据:

import Foundation

class FlightDataModel {
var airline: String?
var price: String?

init(airline: String?, price: String?) {
self.airline = airline
self.price = price
}
}

Controller .swift

import Foundation
import UIKit


class FlightsController: UITableViewController, UITableViewDataSource {


var arrayOfFlights : [FlightDataModel] = [FlightDataModel]()

var startpoint:String!
var endpoint:String!


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.arrayOfFlights.count

}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as myCustomTableViewCell

cell.airlineLabel.text = arrayOfFlights[indexPath.row].airline
cell.priceLabel.text = arrayOfFlights[indexPath.row].price

return cell


}



override func viewDidLoad() {
super.viewDidLoad()

// json request to api

let qpxRequest = Router.QPXRequest()

request(qpxRequest).responseJSON { (request, response, json, error) -> Void in

if json != nil {
//insert airline data into arrayOfFlights
if let myJSON = json as? [String:AnyObject] {
if let trips = myJSON["trips"] as? [String:AnyObject] {
if let data = trips["data"] as? [String:AnyObject] {

if let carriers = data["carrier"] as? [[String:String]] {
for (index, carrierName) in enumerate(carriers) {

var myFlight = FlightDataModel(airline: carrierName["name"] as String!, price:nil)
self.arrayOfFlights.append(myFlight)

}
}
}
if let tripOptions = trips["tripOption"] as? [[String:AnyObject]] {
for (index, tripOption) in enumerate(tripOptions) {

self.arrayOfFlights[index].price = tripOption["saleTotal"] as String!

}
}
}
}


}

} // end api request

} //end viewdidload

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

}

最佳答案

确保您的类设置为 tableViewdataSource

并在完成数据解析后调用方法 tableView.reloadData()

关于swift - 为什么我的数据没有显示在表格 View 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30199941/

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