0

I have the following swift3 code:

    var tempNames = ["Alan","Dan","Mark","Jack","Kelly","Andrew","Paul"]

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)

    let tempName = tempNames[indexPath.row]
    cell.textLabel?.text = tempName
    cell.detailTextLabel?.text = "Score"

    return cell
}

Which produces the following:

enter image description here

As you can see the names are static and the score simply is the word "score". The following JSON is returned from "https://str8red.com/jsonoverallleaderboard/":

[["shanu", "1056"], ["snookro", "828"], ["tingeypa", "709"], ["shaun", "620"], ["chrakers", "506"]] 

What I would like to do is used the names and score from the above JSON instead of the static name and score already in use.

I have had a look around on stackoverflow and followed some guides without success. Any help on this would be appreciated, Alan.

Alan Tingey
  • 835
  • 11
  • 39
  • Possible duplicate of [Correctly Parsing JSON in Swift 3](https://stackoverflow.com/questions/39423367/correctly-parsing-json-in-swift-3) – vadian Jul 07 '17 at 12:43
  • Many thanks Vadian, I have seen this link but am having difficulty applying the theory in practice. – Alan Tingey Jul 07 '17 at 12:50

1 Answers1

1
  • Create a struct

    struct Player {
        let name : String
        var score : String
    }
    
  • In the view controller create a data source array

    var players = [Player]()
    
  • After parsing the JSON having the array in the question (assuming it's in the variable jsonPlayers of type [[String]]) map the array to the struct

    players = jsonPlayers.map { Player(name: $0[0], score: $0[1]) }
    
  • In numberOfRows return

    return players.count
    
  • In cellForRow assign the values to the labels

    let player = players[indexPath.row]
    cell.textLabel?.text = player.name
    cell.detailTextLabel?.text = player.score
    
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Many thanks, I will crack on with the parse part on try the above shortly. – Alan Tingey Jul 07 '17 at 12:55
  • The *parse part* is described in detail in my answer of the linked question. Learn to read the JSON. – vadian Jul 07 '17 at 12:57
  • Hello Vadian, I have put it all together but I can't work out what to put where :/ I get it working if I pop it all in the override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell section but it is so slow. If i pop it in the class OverallLeagueVC: UITableViewController { section it just errors :/ – Alan Tingey Jul 08 '17 at 12:01
  • In `cellForRow` put only the code which updates the UI, nothing else. Put the code to load the data in `viewDidLoad` or `viewWillAppear` depending on if the data is supposed to be loaded once or on each controller switch. – vadian Jul 08 '17 at 12:05
  • Ok, that is making sense. But when I pop the code to load the data in ViewDidLoad it errors whenever I try to use players. For example, in the numberOfRowsInSection I try to use players.count but it says "use of unresolved identifier 'players'". Appreciate the support and apologise if this is a stupid question. – Alan Tingey Jul 08 '17 at 12:12
  • The data source array `players` must be declared on the top level of the (controller) class, the struct can be declared inside or outside the controller class, outside if it's going to be used somewhere else. – vadian Jul 08 '17 at 12:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148681/discussion-between-alan-tingey-and-vadian). – Alan Tingey Jul 08 '17 at 12:25