0

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
}
Sh_Khan
  • 86,695
  • 6
  • 38
  • 57
Lord PB
  • 15
  • 3

2 Answers2

0

You have to set dataSource in viewDidLoad

self.tableView.dataSource = self

//

class ViewController: UIViewController, UITableViewDelegate , UITableViewDataSource {....}
Sh_Khan
  • 86,695
  • 6
  • 38
  • 57
  • I get {Type 'ViewController' does not conform to protocol 'UITableViewDataSource'} – Lord PB Mar 23 '18 at 22:55
  • It didn't let me override the func, what else could be the issue? – Lord PB Mar 24 '18 at 00:35
  • is there a red icon with white color inside ?? double click it to automatically write the methods – Sh_Khan Mar 24 '18 at 00:36
  • [link]https://d1ro8r1rbfn3jf.cloudfront.net/ms_171157/xtaKoElMsiHWuhVjsC4gY9cJlOGsMs/ViewController.swift%2B%25E2%2580%2594%2BEdited%2B2018-03-24%2B00-43-49.png?Expires=1521938637&Signature=ecd-zfI12D2xHo13r1gaXWRqrzxS6Me13mFyWAaTGmniEEsevLzrNhgCEkZjJEQivQOIuCUr60~FXREJXmI-oaBvu1YrKQHdOKLQjDKkfaHvgdzwJM5QK~8LnQAqvoCgU9E78aicc5RwuMfRxcOCc-1bX0O-5UQNdBG6~L5Z~9O5WK0lseHrVxJp~bu9Sgx~rJoOrBoWYaO-EHMfx61GZ9v985~JN-8i7Briyt~vjxWXx-K1eimRasXe70sWLwM9vhwqwohchhEeOBG9dL37p4vkjlAwHBdvqFdn1~gSoZJY~oF8n1-pSF4wpS4BhfwN~AoqUYmlL1Uh1Qxr7Y4drw__&Key-Pair-Id=APKAJHEJJBIZWFB73RSA – Lord PB Mar 24 '18 at 00:44
  • ok can you write func tableView and select from the menu that pops the 2 functions and write the current code in them (copy-paste) – Sh_Khan Mar 24 '18 at 00:49
  • First of all, implement table View correctly. See the example of setting up a table view : https://stackoverflow.com/a/33234181/7698092 Then inside your alamofire function use main thread to reload table view like DispatchQueue.main.async { self.tableView.reloadData() } – Awais Fayyaz Mar 24 '18 at 05:49
0

Below is the fixed code

lass ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

@IBOutlet var tableView: UITableView!

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

override func viewDidLoad() {
super.viewDidLoad()

 self.tableView.dataSource = nil
   downloadJSON()

}
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.dataSource = self

self.tableView.reloadData()

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

    self.tableView.reloadData()
}

}