Alan Tingey
Alan Tingey

Reputation: 961

JSON to TableView difficulty

I am having difficulty populating a tableview from a json result. My code is as follows (apologies but it does not seem to want to want to put the first two lines as code :/):

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

override func viewDidLoad()
{
    super.viewDidLoad()

    let url = NSURL(string: "http://api.football-data.org/alpha/soccerseasons/354/teams")
    let data = NSData(contentsOfURL: url!)
    let json = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSDictionary

    let teamsArray = json["teams"] as NSArray

    print("Team List : \(teamsArray)")

    for dic in teamsArray
    {
        let teamname = dic["name"] as NSString

        let code = dic["code"] as NSString

        println("Team Name, \(teamname) : Code, \(code)")
    }

    //        self.tableViewObject.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}

@IBOutlet weak var tableViewObject: UITableView!




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



func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
    cell.textLabel!.text = teamsArray[indexPath.row]


    return cell
}

}

It is complaining twice about "Use of unresolved identifier 'teamsArray' twice. First at:

return teamsArray.count

and then at:

cell.textLabel!.text = teamsArray[indexPath.row]

Can someone help me link my JSON with my tableview please by helping me resolve the error mentioned above or putting me in the correct direction.

It is worth noting when I use the code on a blank view without any hint of a tableview I get perfect results in the console:

    let url = NSURL(string: "http://api.football-data.org/alpha/soccerseasons/354/teams")
    let data = NSData(contentsOfURL: url!)
    let json = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSDictionary

    let teamsArray = json["teams"] as NSArray

    print("Team List : \(teamsArray)")

    for dic in teamsArray
    {
        let teamname = dic["name"] as NSString

        let code = dic["code"] as NSString

        println("Team Name, \(teamname) : Code, \(code)")
    }

I am new to stackoverflow and was told my question earlier was not specific enough. Please let me know if this is still too vague and I will try to improve on it.

Many thanks, Alan.

Upvotes: 0

Views: 100

Answers (1)

Bhoomi Jagani
Bhoomi Jagani

Reputation: 2423

Do like this...

  import UIKit

class ViewController:   UIViewController,UITableViewDelegate,UITableViewDataSource {

 @IBOutlet weak var tableViewObject: UITableView!
 var teamsArray = NSArray()
 override func viewDidLoad()
{
super.viewDidLoad()

self.tableViewObject.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

let url = NSURL(string: "http://api.football-data.org/alpha/soccerseasons/354/teams")
let data = NSData(contentsOfURL: url!)
let json = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSDictionary

teamsArray = json["teams"] as NSArray

print("Team List : \(teamsArray)")

self.tableViewObject.reloadData()

for dic in teamsArray
{
    let teamname = dic["name"] as NSString

    let code = dic["code"] as NSString

    println("Team Name, \(teamname) : Code, \(code)")
}


}




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



 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
cell.textLabel!.text = self.teamsArray.objectAtIndex(indexPath.row).objectForKey("name") as? NSString


return cell
}

Upvotes: 1

Related Questions