Swift 3 / Alamofire:我无法获取UITableView上的数据

时间:2017-06-29 00:27:23

标签: uitableview swift3 alamofire

我正在使用Alamofire并尝试在我的tableview上获取数据,但是我无法获取数据。当我使用cmd Print时,它显示数据但无法获取数据。如何在tableview上获取数据?

请找到以下代码: -

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
}

}

谢谢!

2 个答案:

答案 0 :(得分:1)

需要像这样更改loadGroups函数

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)")
            }
    }
}

答案 1 :(得分:0)

阵列&#39;组&#39;附加了Alamofire的回复,但Array&#39; arrRes&#39;用作表视图数据源。如果在数据源方法中使用self.group而不是arrRes,则表应使用Alamofire响应中收到的新组进行更新。