TableView不会在Swift 3中填充JSON数组

时间:2017-01-19 15:04:24

标签: arrays json uitableview swift3

我在这里已经阅读了很多相关的问题,但我仍然在努力填写我的桌面视图。我从控制台中的print(firstname)和print(lastname)得到结果。但是桌面视图中没有任何内容。 Table视图有一个Prototype单元格,标识符为BasicCell。 知道桌子没有填充的原因吗?

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var listTableView: UITableView!
let URL_GET_PERSONAGE = "http://somesite.net/somescript.php"
var firstnames: [String] = []
var lastnames: [String] = []

override func viewDidLoad() {
    super.viewDidLoad()
    let url = URL(string: URL_GET_PERSONAGE)
    do{
        let allContactsData = try Data(contentsOf: url!)
        let allContacts = try JSONSerialization.jsonObject(with: allContactsData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String : AnyObject]
        if let arrJSON = allContacts["teams"] as? NSArray{
            for index in 0..<arrJSON.count{
                let aObject = arrJSON[index] as? [String : AnyObject]
                firstnames.append(aObject?["fname"] as! String)
                lastnames.append(aObject?["lname"] as! String)

            }
            print(firstnames)
            print(lastnames)
            listTableView.reloadData()
        }
    }
    catch {
    }
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return self.firstnames.count;
    }
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell: UITableViewCell! = self.listTableView.dequeueReusableCell(withIdentifier: "BasicCell", for: indexPath as IndexPath) as UITableViewCell!
        cell.textLabel?.text = self.firstnames[indexPath.row]

        return cell
    }
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}    
}

0 个答案:

没有答案