2016-02-19 10 views
8

Ich habe den folgenden Code.Füllen von Tabellenansichtszellen aus JSON mit Alamofire (Swift 2)

import UIKit 
import Alamofire 

class CheHappyTableViewController: UITableViewController, NSURLConnectionDelegate { 

    var happyHours = [HappyHour]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     //Load the cell elements 
     loadHappyHourElements() 
    } 

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

    func loadHappyHourElements(){ 

     let testhappyhour:HappyHour = HappyHour(title: "TEST", image: "TESST", description: "TEST", postedDate: "TEST") 
     self.happyHours.append(testhappyhour) 

     let url:String = "https://gist.githubusercontent.com/arianitp/036133ebb5af317a595c/raw/f134ec241ec3126bedd6debe5de371e3e01d225b/happyhours.json" 
     Alamofire.request(.GET, url, encoding:.JSON).responseJSON 
      { response in switch response.result { 
      case .Success(let JSON): 
       let response = JSON as! NSArray 
       for item in response { // loop through data items 
        let obj = item as! NSDictionary 
        let happyhour = HappyHour(title:obj["title"] as! String, image:obj["image"] as! String, description:obj["description"] as! String, postedDate:obj["date"] as! String) 
        self.happyHours.append(happyhour) 
       } 

      case .Failure(let error): 
       print("Request failed with error: \(error)") 
       } 
     } 
     self.tableView.reloadData() 
    } 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 1 
    } 

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

    //Displays the cells in the table 
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     self.tableView.rowHeight = UIScreen.mainScreen().bounds.size.width 
     let cellIdentifier = "CheHappyTableViewCellController" 
     let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CheHappyTableViewCellController 

     let happyHour = happyHours[indexPath.row] 
     cell.lblTitle.text = happyHour.title 
     //cell.cheHappyImage = happyHour.photo 

     // Configure the cell... 
     return cell 
    } 
} 

Die Tabellenzellen aktualisiert werden nicht, obwohl ich den self.tableView.reloadData() in der Alamofire Anfrage komplette Funktion enthalten. Ich habe auch ein Beispielobjekt mit Titel definiert und alle Eigenschaften auf "TEST" gesetzt, das wird geladen, aber die JSON-Datei füllt die Tabelle nicht auf. Ich kann sehen, dass die Datei korrekt heruntergeladen und gelesen wird, aber ich denke, die Elemente werden entweder nicht zur Eigenschaftsvariablen happyHours hinzugefügt oder irgendwie werden die Elemente nicht neu geladen.

Ich habe viele Lösungen von hier versucht, aber ohne Erfolg. Was mache ich falsch?

Antwort

11

Ihre self.tableView.reloadData() Zeile ist außerhalb des Callbacks, dh sie wird sofort aufgerufen, bevor die Daten geladen wurden. Versuchen Sie folgendes:

func loadHappyHourElements(){ 

    let testhappyhour:HappyHour = HappyHour(title: "TEST", image: "TESST", description: "TEST", postedDate: "TEST") 
    self.happyHours.append(testhappyhour) 

    let url:String = "https://gist.githubusercontent.com/arianitp/036133ebb5af317a595c/raw/f134ec241ec3126bedd6debe5de371e3e01d225b/happyhours.json" 
    Alamofire.request(.GET, url, encoding:.JSON).responseJSON 
     { response in switch response.result { 
     case .Success(let JSON): 
      let response = JSON as! NSArray 
      for item in response { // loop through data items 
       let obj = item as! NSDictionary 
       let happyhour = HappyHour(title:obj["title"] as! String, image:obj["image"] as! String, description:obj["description"] as! String, postedDate:obj["date"] as! String) 
       self.happyHours.append(happyhour) 
      } 
      self.tableView.reloadData() 

     case .Failure(let error): 
      print("Request failed with error: \(error)") 
      } 
    } 
} 
+1

Lebensretter! Danke, das hat wie ein Zauber funktioniert. –

+1

@ RinorBytyçi Kein Problem, ein einfacher Fehler zu machen! :) Btw, versuche mit [Alamofire-SwiftyJSON] (https://github.com/SwiftyJSON/Alamofire-SwiftyJSON) die Handhabung der JSON-Antwort zu erleichtern. – Tometoyou