为什么我的数据没有显示在tableview中?

时间:2022-09-26 23:02:04

Printing out the data in the console works just fine. In viewdidload method the array count is also fine. But if i use any of the code outside viewdidload its not working. Im trying to populate a table with two labels in each cell (airline and price) but none of the data is displaying and when i try out in the console arrayOfFlights.price.count (for example) it prints out 0 as if its empty.

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

Data:

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
}

}

1 个解决方案

#1


Make sure your class is set as dataSource of your tableView

确保您的类被设置为tableView的dataSource

And call the method tableView.reloadData() when you are done with the data parsing.

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

#1


Make sure your class is set as dataSource of your tableView

确保您的类被设置为tableView的dataSource

And call the method tableView.reloadData() when you are done with the data parsing.

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