2016-06-12 18 views
0

Ich habe ein Problem bei der Anzeige eines JSON-Arrays in einem TableView. Das Ergebnis ist eine leere Tabelle, die korrekt eingerichtet ist, da sie mit einem festen Datenarray arbeitet. Ich scheine in der Lage zu sein, den JSON korrekt zu analysieren, da ich in der Lage bin, die Druckausgabe in der Konsole zu manipulieren. Ich denke, das Problem ist eine Reload-Tabellenfunktion, aber ich habe jede Variation einer Lösung versucht, die ich online gefunden habe Mach es zur Arbeit. Die tatsächliche JSON sieht wie folgt aus:Problem beim Anzeigen von JSON-Daten in Swift TableView

[{ 
"locationNumber": 10, 
"locationName": "Test Site", 
"locationPhoneNumber": "", 
"locationAddress": "test address" 
}, 
{ 
"locationNumber": 99, 
"locationName": "Test Site2", 
"locationPhoneNumber": "", 
"locationAddress": "second test address" 
}] 

The LocationTableViewController

import UIKit 
import Alamofire 
import AlamofireObjectMapper 

class LocationTableViewController: UITableViewController { 

var locations = [LocationResponse]() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    getLocationResponse() 
} 

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

// MARK: - Table view data source 
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
      return 1} 

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cellIndentifier = "Cell" 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIndentifier, forIndexPath: indexPath) as! LocationTableViewCell 

    //configure the cell 
    cell.locationLabel.text = locations[indexPath.row].locationName 
    cell.locationidLabel.text = "$\(locations[indexPath.row].locationID)" 
    return cell 
} 

func getLocationResponse() { 
    let URL = "https://test.com.au/api/Store?uid=7654380A-D18F-46A1&username=app&password=apptest" 
    Alamofire.request(.GET, URL).responseArray { (response: Response<[LocationResponse], NSError>) in 
     let locationArray = response.result.value 
     if let locationArray = locationArray { 
      for location in locationArray { 
       print(location.locationID) 
       print(location.locationName) 
      } 
     } 
    } 
} 

Die LocationResponse.swift

import Foundation 
import ObjectMapper 

class LocationResponse: Mappable { 
var locationName: String? 
var locationID: Int? 

required init?(_ map: Map){ 
} 

func mapping(map: Map) { 
    locationName <- map["locationName"] 
    locationID <- map["locationNumber"] 
} 
} 

Individuelle Cell:

import UIKit 

class LocationTableViewCell: UITableViewCell { 

@IBOutlet var locationLabel: UILabel! 
@IBOutlet var locationidLabel: UILabel! 

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 
} 

} 

Ich erhalte diese in der Konsole von der Druckfunktion, die korrekt aussieht und ich denke, bedeutet das Array korrekt erstellt wurde:

Optional(10) 
Optional("Test Site") 
Optional(99) 
Optional("Test Site2") 

Bitte helfen Sie, wenn Sie können

Antwort

1

Sie nie das Ergebnis vom Server zuweisen die var locations = [LocationResponse]() Variable und es ist immer leer.

if let locationArray = locationArray { 
    self.locations = locationArray 
    self.tableView.reloadData() 
} 
+0

behoben! ich danke dir sehr! – Hound

+0

jetzt akzeptiert, danke nochmal – Hound

+0

(Und Sie sollten zurückkommen, sobald Sie genug rep und up-vote Benutzerantwort haben. Sie müssen nicht, aber es zeigt Dankbarkeit.) –