0

I've got 3 labels in a custom UITableview cell and I'm trying to pass in json data I've gotten from an api with Alamofire but I'm struggling to understand how to push the returned json into the tableview. Any help would be greatly appreciated. Code below:

import UIKit
import Parse
import Alamofire

class LeagueTableController: UIViewController, UITableViewDataSource,     UITableViewDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    Alamofire.request(.GET, "https://api.import.io/store/connector/88c66c----9b01-6bd2bb--d/_query?input=webpage/url:----") .responseJSON { response in // 1
        print(response.request)  // original URL request
        print(response.response) // URL response
        print(response.data)     // server data
        print(response.result)   // result of response serialization

        if let JSON = response.result.value {
            print("JSON: \(JSON)")
        }
    }
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    return cell
}


}

Returned json like this:

  {
connectorGuid = "88c66cb4-e64f-4316-9b01-6bd2bb2d762d";
connectorVersionGuid = "8aedfe43-948a-4559-b279-d3c3c28047a4";
cookies =     (
);
offset = 0;
outputProperties =     (
            {
        name = team;
        type = URL;
    },
            {
        name = played;
        type = DOUBLE;
    },
            {
        name = points;
        type = DOUBLE;
    }
);
pageUrl = "http://www.extratime.ie/leagues/2024/100/premier-division/";
results =     (
            {
        played = 9;
        "played/_source" = 9;
        points = 22;
        "points/_source" = 22;
        team = "http://www.extratime.ie/squads/17/";
        "team/_source" = "/squads/17/";
        "team/_text" = Dundalk;
    },
            {
        played = 9;
        "played/_source" = 9;
        points = 20;
        "points/_source" = 20;
        team = "http://www.extratime.ie/squads/7/";
        "team/_source" = "/squads/7/";
        "team/_text" = "Derry City";
    },
            {
        played = 9;
        "played/_source" = 9;
        points = 17;
        "points/_source" = 17;
        team = "http://www.extratime.ie/squads/100504/";
        "team/_source" = "/squads/100504/";
        "team/_text" = "Galway United FC";
    },
            {
        played = 9;
        "played/_source" = 9;
        points = 16;
        "points/_source" = 16;
        team = "http://www.extratime.ie/squads/29/";
        "team/_source" = "/squads/29/";
        "team/_text" = "St. Patrick's Ath";
    },
            {
        played = 8;
        "played/_source" = 8;
        points = 15;
        "points/_source" = 15;
        team = "http://www.extratime.ie/squads/30/";
        "team/_source" = "/squads/30/";
        "team/_text" = "Cork City";
    },
            {
        played = 8;
        "played/_source" = 8;
        points = 15;
        "points/_source" = 15;
        team = "http://www.extratime.ie/squads/3/";
        "team/_source" = "/squads/3/";
        "team/_text" = "Shamrock Rovers";
    },
            {
        played = 9;
        "played/_source" = 9;
        points = 10;
        "points/_source" = 10;
        team = "http://www.extratime.ie/squads/13/";
        "team/_source" = "/squads/13/";
        "team/_text" = "Finn Harps";
    },
            {
        played = 9;
        "played/_source" = 9;
        points = 10;
        "points/_source" = 10;
        team = "http://www.extratime.ie/squads/2/";
        "team/_source" = "/squads/2/";
        "team/_text" = Bohemians;
    },
            {
        played = 9;
        "played/_source" = 9;
        points = 7;
        "points/_source" = 7;
        team = "http://www.extratime.ie/squads/8/";
        "team/_source" = "/squads/8/";
        "team/_text" = "Sligo Rovers";
    },
            {
        played = 9;
        "played/_source" = 9;
        points = 7;
        "points/_source" = 7;
        team = "http://www.extratime.ie/squads/6/";
        "team/_source" = "/squads/6/";
        "team/_text" = "Bray Wanderers";
    },
            {
        played = 9;
        "played/_source" = 9;
        points = 5;
        "points/_source" = 5;
        team = "http://www.extratime.ie/squads/109/";
        "team/_source" = "/squads/109/";
        "team/_text" = "Wexford Youths";
    },
            {
        played = 9;
        "played/_source" = 9;
        points = 5;
        "points/_source" = 5;
        team = "http://www.extratime.ie/squads/15/";
        "team/_source" = "/squads/15/";
        "team/_text" = "Longford Town";
    }
);

} I'm trying to just push the "played", "points" and "team/_text" results out to each of the labels.

ShaneN
  • 7
  • 8

3 Answers3

1

Since the question is very broad and doesn't specify what exactly is the problem here, The general steps are:

1) map your json to dictionary/nsdictionary. Suppose the JSON snippet you posted is a chunk of JSONArray in following format [{}], all you need to do is:

var arrayOfDictionaries:NSArray = NSJSONSerialization.JSONObjectWithData(yourData, options: nil, error: nil) as! NSArray

where yourData variable is data downloaded from network casted to NSData format

2) create outlets to these three labels in your custom tableViewCell

3) for each cell, set these labels inside

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)

method as follows:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  let cell:YourCustomCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as YourCustomCell
  cell.firstLabel.text = yourDictionary["played"]
  cell.secondLabel.text = yourDictionary["points"]
  cell.thirdLabel.text = yourDictionary["team"]
  return cell
}

4) I suppose you will need more cells than just one, then store many dictionaries in array and access each element like this:

cell.firstLabel.text = arrayOfDictionaries[indexPath.row]["played"]
theDC
  • 6,364
  • 10
  • 56
  • 98
  • Think it will, try it out – theDC Apr 26 '16 at 08:44
  • Sure, come back with a feedback and please accept the answer if you find it helpful – theDC Apr 27 '16 at 09:35
  • yourData would your downloaded data, in this case response.data as NSData, the latter goes inside func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath), as I said in my answer – theDC Apr 27 '16 at 15:57
  • declare a class variable called myData:NSData then in view did load assing response.data to it and use it inside the function where you serialize json. You need to put some effort to resolve this by yourself. I really posted everything you need to know to make it done. Please be more patient and try by yourself since it's hard to reproduce entire problem – theDC Apr 29 '16 at 11:05
  • Yes I totally understand that, no need to be sorry but I encourage you to search wider beyond this thread, e.q "What does unresolved identifier ios means" etc. I hope you understand what I mean :) – theDC Apr 29 '16 at 11:39
0

You'll need to create a subclass of UITableViewCell, say MyTableViewCell and add a property named JSON.

Now, since you're probably using Interface Builder to define your cell and its reuse identifier ("Cell"), set that cell's class to your newly created MyTableViewCell and connect the labels to some IBOutlets in your newly defined class.

Then, when you call 'dequeueReusableCellWithIdentifier', cast the cell to MyTableViewCell and set its JSON property to the value you want to have in the cell. You'll probably want to react to the change, so add the didSet property observer.

var JSON:[String: AnyObject] = [String: AnyObject]() {
    didSet {
        print("populate your labels with your new data");
    }
}
Zoltán
  • 1,422
  • 17
  • 22
0

First, you should create a model that contain 3 properties that you want to save, like:

    class Data {
    var team = ""
    var point = 0
    var teamText = ""

    init(fromJSON json: NSDictionary) {
        team = json["team"] as! String
        point = json["points"] as! Int
        teamText = json["team/_text"] as! String
    }
}

In your LeagueTableController, create an array to hold the data and show it to tableView:

var data = [Data]()

Config the tableView to show the data:

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! YourCustomCell
    let row = indexPath.row
    cell.lblA.text = data[row].team
    cell.lblB.text = "\(data[row].point)"
    cell.lblC.text = data[row].teamText
    return cell
}

Finally, parse your response json to our data array to display onto tableView

@IBOutlet weak var tableView: UITableView!
var data = [Data]()

override func viewDidLoad() {
    super.viewDidLoad()

    Alamofire.request(.GET, "https://api.import.io/store/connector/88c66c----9b01-6bd2bb--d/_query?input=webpage/url:----") .responseJSON { response in // 1
        print(response.request)  // original URL request
        print(response.response) // URL response
        print(response.data)     // server data
        print(response.result)   // result of response serialization

        if let JSON = response.result.value {
            print("JSON: \(JSON)")
            // parse JSON to get array of data
            let array = JSON["results"] as! [NSDictionary]
            // map an item to a data object, and add to our data array
            for item in array {
                self.data.append(Data(fromJSON: item))
            }
            // after mapping, reload the tableView
            self.tableView.reloadData()
        }
    }
}

Hope this help!

maphongba008
  • 2,114
  • 4
  • 20
  • 33