Hound Hound - 2 years ago 84
iOS Question

Issue displaying JSON data in Swift TableView

I'm having an issue displaying a JSON array in a TableView - result is a blank table which i know is set up correctly as it works with a fixed data array. I seem to be able to parse the JSON correctly as i'm able to manipulate the print output in the console, I think the problem is a reload table function however I have attempted every variation of a solution I have found online and cannot seem to get it to work.
The actual JSON looks like this:

"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() {

override func 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 {

The 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"]

Custom Cell:

import UIKit

class LocationTableViewCell: UITableViewCell {

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

override func awakeFromNib() {
// Initialization code

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


I'm getting this in the console from the print function which looks correct and I guess means the array has been created correctly:

Optional("Test Site")
Optional("Test Site2")

Please help if you can

Answer Source

You never assign the result from server to the var locations = [LocationResponse]() variable and it's always empty.

if let locationArray = locationArray {
   self.locations = locationArray
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download