0

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

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() {
    super.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(10)
Optional("Test Site")
Optional(99)
Optional("Test Site2")

Please help if you can

1 Answer 1

1

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
   self.tableView.reloadData()
}
Sign up to request clarification or add additional context in comments.

1 Comment

(And you should come back once you have enough rep and up-vote user's answer. You don't have to, but it shows gratitude.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.