Search code examples
iosalamofireswift4swifty-json

Swift 4 UITableView not showing JSON results


So my issue is that my JSON isn't showing in my tableView but it is printing in the terminal just fine. I have tried a few things but can't seem to get this to print in the Table at all. What could my issue be?

import UIKit
import Alamofire
import SwiftyJSON

class ViewController: UIViewController, UITableViewDelegate {


@IBOutlet var tableView: UITableView!

var arrRes = [[String:AnyObject]]() //Array of dictionary


override func viewDidLoad() {
    super.viewDidLoad()
    downloadJSON()
    self.tableView.reloadData()
    }


func downloadJSON() {
    Alamofire.request(API_URL).responseJSON { (responseData) -> Void in
        if ((responseData.result.value) != nil) {
            let swiftyJsonVar = JSON(responseData.result.value!)

            if let resData = swiftyJsonVar["race_results"].arrayObject {
                self.arrRes = resData as! [[String: AnyObject]]
                print("resData", resData)
                if self.arrRes.count > 0 {
                    print("Table Updating")
                    print("Table should be updated")
                    self.tableView.reloadData()

                }
            }
            print("new array3")
            print(self.arrRes.count)
        }
        print("new array 2")
        print(self.arrRes.count)
        self.tableView.reloadData()
    }
}


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



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> TableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
    var dict = self.arrRes[indexPath.row]
    cell.name.text = dict["Full_Name"] as! String
    cell.club.text = dict["Club"] as! String
    cell.position.text = dict["Finish_Position"] as! String
    return cell
}

Solution

  • You have to set dataSource in viewDidLoad

    self.tableView.dataSource = self
    

    //

    class ViewController: UIViewController, UITableViewDelegate , UITableViewDataSource {....}