Kunal Parekh
Kunal Parekh

Reputation: 370

Swift 3 / Alamofire : I am not able to fetch the data on UITableView

I am using Alamofire and trying to fetch data on my tableview, however I am not able to get the data. When I use the cmd Print, its showing me the data but not able to fetch the data. How can I fetch the data on my tableview?

Please find the code below:-

import UIKit
import Alamofire
import SwiftyJSON


class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSURLConnectionDelegate {
//let myarray = ["item1", "item2", "item3"]
var tableData = Array<Group>()

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



var group = [Group]()

@IBOutlet weak var tableview: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    loadGroups()
}

func loadGroups(){

    let testhappyhour:Group = Group(tempName: "TEST", tempID: "TESST", icons: "TEST", tempgbcount: "TEST")
    self.group.append(testhappyhour)

    //let groupQuery:String = "http://jsonplaceholder.typicode.com/users"
    Alamofire.request("http://jsonplaceholder.typicode.com/users").responseJSON
        { response in switch response.result {
        case .success(let JSON):
            let response = JSON as! NSArray
            for item in response { // loop through data items
                let obj = item as! NSDictionary
                let happyhour = Group(tempName:obj["NAME"] as! String, tempID:obj["id"] as! String, icons:obj["icon"] as! String, tempgbcount:obj["TOTAL"] as! String)
                self.group.append(happyhour)
            }
            self.tableview.reloadData()

        case .failure(let error):
            print("Request failed with error: \(error)")
            }
    }
}


func convertToArray(text: String) -> [Any]? {
    if let data = text.data(using: .utf8) {
        do {
            return try JSONSerialization.jsonObject(with: data, options: []) as? [Any]
        } catch {
            print(error.localizedDescription)
        }
    }
    return nil
}

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: "groupCell", for: indexPath) as! UITableViewCell
   // cell.textLabel?.text = myarray[indexPath.item]
    let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "groupCell")!
    var dict = arrRes[indexPath.row]
    cell.textLabel?.text = dict["NAME"] as? String
    cell.detailTextLabel?.text = dict["TOTAL"] as? String
    return cell
}

}

Thank you!!

Upvotes: 0

Views: 309

Answers (2)

Usama Sadiq
Usama Sadiq

Reputation: 609

Need to change loadGroups function like this

func loadGroups(){

    let testhappyhour:Group = Group(tempName: "TEST", tempID: "TESST", icons: "TEST", tempgbcount: "TEST")
    self.group.append(testhappyhour)

    Alamofire.request("http://jsonplaceholder.typicode.com/users").responseJSON
        { response in switch response.result {
        case .success(let JSON):
            let response = JSON as! NSArray
            for item in response { // loop through data items
                let obj = item as! NSDictionary
                let happyhour = Group(tempName:obj["NAME"] as! String, tempID:obj["id"] as! String, icons:obj["icon"] as! String, tempgbcount:obj["TOTAL"] as! String)
                self.group.append(happyhour)
                self.arrRes.append(obj) // ADD THIS LINE
            }
            self.tableview.reloadData()

        case .failure(let error):
            print("Request failed with error: \(error)")
            }
    }
}

Upvotes: 1

FryAnEgg
FryAnEgg

Reputation: 493

Array 'group' is appended with the Alamofire responses, but Array 'arrRes' is used as the table view data source. If you use self.group instead of arrRes in the data source methods, the table should update with the new groups received in the Alamofire response.

Upvotes: 0

Related Questions