gpt4 book ai didi

ios - ViewTable不读取数组(IOS)

转载 作者:行者123 更新时间:2023-11-29 01:04:09 24 4
gpt4 key购买 nike

我有 2 个程序,第一个程序在线读取 JSON 并将其保存到数组中。第二个程序使用数组完成 UITableView,但是当我加入时不起作用。

当我调试时,我先看表,但我先看子方法。

class ViewController: UIViewController, UITableViewDataSource,    UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

var TableData:Array< String > = Array < String > ()

let textCellIdentifier = "TextCell"


override func viewDidLoad() {
super.viewDidLoad()


//--------------------------------------------------------------------------------------------------

let requestURL: NSURL = NSURL(string: "http://www.learnswiftonline.com/Samples/subway.json")!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(urlRequest) {
(data, response, error) -> Void in

let httpResponse = response as! NSHTTPURLResponse
let statusCode = httpResponse.statusCode

if (statusCode == 200) {

do{

let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)

if let stations = json["stations"] as? [[String: AnyObject]] {

for station in stations {

if let name = station["stationName"] as? String {

if let year = station["buildYear"] as? String {
self.TableData.append(name + "[" + year + "]")
}

}
}

}

}catch {
print("Error with Json: \(error)")

}

/* for element in TableData {
print(element)
}*/

}




}
task.resume()



//--------------------------------------------------------------------------------------------------


tableView.delegate = self
tableView.dataSource = self
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


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

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath)

let row = indexPath.row
cell.textLabel?.text = TableData[row]

return cell
}

}

在我的模拟器上只看到空表

最佳答案

这是因为您没有重新加载 tableView。 HTTP 请求需要一些时间才能完成。

一旦您的请求完成调用 self.tableView.reloadData()

它将在另一个队列上,所以使用

 NSOperationQueue.mainQueue().addOperationWithBlock { () -> Void in
self.tableView.reloadData()
}

放置它

// Stuff above...

if let stations = json["stations"] as? [[String: AnyObject]] {

for station in stations {

if let name = station["stationName"] as? String {

if let year = station["buildYear"] as? String {
self.TableData.append(name + "[" + year + "]")
}
}
}

// Do your reload here.. as your array will have finished populating
}

// Stuff below..

关于ios - ViewTable不读取数组(IOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36622550/

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