2016-05-26 11 views
0

Ich kann das Array erfolgreich mit meinen JSON-Daten innerhalb der Schleife füllen, und ich versuche, die Zellen meines TableView mit den gleichen Informationen zu füllen, aber derzeit Die Liste enthält keinen Inhalt.Abrufen von JSON-Daten zum Auffüllen eines TableView von einem Array

import UIKit 

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    @IBOutlet weak var table: UITableView! 

    var headlines = [String]() 
    let baseURL = "http://api.nytimes.com/svc/topstories/v1/business.json?api-key=123456789" 



    override func viewDidLoad() { 
     getJSON() 
     super.viewDidLoad() 
     self.table.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 
     self.table.dataSource = self 
     self.table.delegate = self 
    } 

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


    func getJSON() { 

     let url = NSURL(string: baseURL) 
     let request = NSURLRequest(URL: url!) 
     let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration()) 
     let task = session.dataTaskWithRequest(request){ (data, response, error) -> Void in 

      if error == nil { 
       let SwiftyJSON = JSON(data: data!) 
       let theTitle = SwiftyJSON["results"].arrayValue 

       for title in theTitle{ 

        let titles = title["title"].stringValue 
        self.headlines.insert(titles, atIndex: 0) 
        //print("- " + titles) 
       } 

       print(self.headlines) 

      } 

      else { 
       print("there was an error") 
      } 
     } 

     task.resume() 

    } 


    // From the UITAbleViewDataSource 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return headlines.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = self.table.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell 
     cell.textLabel!.text = self.headlines[indexPath.row] 
     return cell 
    } 

    // From the UITableViewDelegate 
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     print("You tapped on cell # \(indexPath.row)") 
    } 
} 

Antwort

0

Die Aufgabe ist asynchron, also, wenn Sie das Array Sie Ihren Tisch

func getJSON() { 

    let url = NSURL(string: baseURL) 
    let request = NSURLRequest(URL: url!) 
    let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration()) 
    let task = session.dataTaskWithRequest(request){ (data, response, error) -> Void in 

     if error == nil { 
      let SwiftyJSON = JSON(data: data!) 
      let theTitle = SwiftyJSON["results"].arrayValue 

      for title in theTitle{ 

       let titles = title["title"].stringValue 
       self.headlines.insert(titles, atIndex: 0) 
       //print("- " + titles) 
      } 

      print(self.headlines) 
      self.table.reloadData() 
     } 

     else { 
      print("there was an error") 
     } 
    } 

    task.resume() 

} 
+0

Dank neu geladen haben geladen haben! Ich habe das vor einer Minute mit einem Freund herausgefunden, ich schätze die Hilfe! –

Verwandte Themen