【问题标题】:Swift 3 / Alamofire : I am not able to fetch the data on UITableViewSwift 3 / Alamofire:我无法获取 UITableView 上的数据
【发布时间】:2017-06-29 00:27:22
【问题描述】:

我正在使用 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
}

}

谢谢!!

【问题讨论】:

    标签: uitableview swift3 alamofire


    【解决方案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)")
                }
        }
    }
    

    【讨论】:

    • 嗨,它确实有效,但是我无法获得 UILabel。我希望它显示在 UILabel 上。是的,非常感谢:)
    • 你说的是这些标签吗? cell.textLabel?.text = dict["NAME"] as?字符串 cell.detailTextLabel?.text = dict["TOTAL"] as?字符串
    • 我有一个名为 GroupCellTableViewCell 的类文件,我已经在其中连接了我的 UILabel。
    • 嗯好的,然后你可以打开一个新问题。解释问题然后我将能够提供帮助。现在我不完全知道问题可能出在哪里。
    • 好吧,我们可以在聊天中进行这个对话
    【解决方案2】:

    Array 'group' 附加了 Alamofire 响应,但 Array 'arrRes' 用作表视图数据源。如果您在数据源方法中使用 self.group 而不是 arrRes,则该表应使用 Alamofire 响应中收到的新组进行更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多