Swift 3 / Alamofire: Cant load the table with the data

Viewed 607

I am trying to load the data on the UItableview, I am able to see the records fetched in the output panel but not able to fetch them on the UItableview.

Please find the code below:

import UIKit
import Alamofire
import SwiftyJSON


class CategoryViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSURLConnectionDelegate {

var tableData = Array<Category>()
var arrRes = [[String:AnyObject]]() //Array of dictionary
var category = [Category]()
var catTitle = ""
var id = ""



@IBOutlet weak var catTitleRec: UILabel!

@IBOutlet weak var tableview: UITableView!




override func viewDidLoad() {
    super.viewDidLoad()

    catTitleRec.text = catTitle
    loadCategories(id:id)
}

func loadCategories(id : String) {


    let userDic : [String : AnyObject] = ["id":id as AnyObject]

    Alamofire.upload(multipartFormData: { multipartFormData in

        for (key, value) in userDic {
            multipartFormData.append(value.data(using: String.Encoding.utf8.rawValue)!, withName: key)
        }

    },to: "http://www.URL.com.au/database/results.php" , method: .post, headers: nil ,
      encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.response { [weak self] response in
                guard self != nil else {
                    return
                }
                debugPrint(response)

            }

            upload.responseJSON {  response in

                print(response)
            //    let responseJSON = response.result.value as! NSDictionary


              //  print(responseJSON)



            }

        case .failure(let encodingError):
            print("error:\(encodingError)")
        }


    })
    tableview.reloadData()
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    tableview.reloadData()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "categoryCell") as! CategoryCellTableViewCell
    var dict = arrRes[indexPath.row]

    cell.catName.text = dict["category.NAME"] as? String
    cell.total?.text = dict["TOTAL"] as? String


    return cell
}

}

and my CategoryCellTableViewCell.swift code:

import UIKit

class CategoryCellTableViewCell: UITableViewCell {

@IBOutlet weak var catName: UILabel!

@IBOutlet weak var total: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

I tried to seek some help online but couldnt get but thanks a lot for the time :)

dict output:

SUCCESS: (
    {
    NAME = "COSMETIC PRODUCTS & SERVICES";
    TOTAL = 1;
    id = 54300;
},
    {
    NAME = TATTOOING;
    TOTAL = 1;
    id = 225700;
}

)

4 Answers
Related