【问题标题】:Whats wrong with this UIViewController? Swift 3, Loading Data这个 UIViewController 有什么问题? Swift 3,加载数据
【发布时间】:2017-06-02 02:02:43
【问题描述】:

我只是想把这个解析的比特币价格数据加载到表格视图中。我现在将其设置为测试字符串,但这仍然无法正常工作。我已经三次检查了故事板中的所有内容,所以我假设它是这段代码中的内容:

import UIKit
import Alamofire
import AlamofireObjectMapper
import ObjectMapper

class Response: Mappable {
    var data: [Amount]?
    required init?(map: Map){

    }

    func mapping(map: Map) {
        data <- map["data"]
    }
}

class Amount: Mappable {
    var data : String?

    required init?(map: Map){

    }

    func mapping(map: Map) {
        data <- map["data.amount"]
    }
}

let mount = [String]()

let am = [String]()

class ViewController: UIViewController, UITableViewDelegate, 
UITableViewDataSource {

    @IBOutlet var tableView: UITableView!

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: 
IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel?.text = "test"

        return cell
    }

    func Call_bitcoin(){
        let url = "https://api.coinbase.com/v2/prices/BTC-USD/buy"
        Alamofire.request(url).responseObject{ (response: DataResponse<Amount>) in
            let mount = response.result.value
            let am = mount?.data
            print(am)

        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        Call_bitcoin()

        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.delegate = self
        tableView.dataSource = self

        self.tableView.reloadData()
    }


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


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("You tapped cell number \(indexPath.row).")
    }
}

【问题讨论】:

  • 您好,请改进您的代码格式。很难按原样阅读...谢谢!
  • 谢谢,修正了格式。向安东大喊!
  • 你的TableView 加载了吗?我的意思是,你能看到细胞分隔线吗?

标签: ios swift uitableview cocoa-touch uikit


【解决方案1】:

您只需要在 Alamofire 请求的回调中调用 reloadData() 方法即可。这是因为 Alamofire 进行了异步调用。

然后,您将拥有像这样的Call_bitcoin

func Call_bitcoin() {
    let url = "https://api.coinbase.com/v2/prices/BTC-USD/buy"
    Alamofire.request(url).responseObject{ (response: DataResponse<Amount>) in
        let mount = response.result.value
        let am = mount?.data
        print(am)
        self.tableView.reloadData()
    }
}

此外,您可以重构 viewDidLoad 方法,因为您不需要重新加载 tableview 的数据(您已经在 Call_bitcoin 方法)。所以,viewDidLoad 会是这样的:

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    tableView.delegate = self
    tableView.dataSource = self
    Call_bitcoin()
}

【讨论】:

    【解决方案2】:

    在成功获取数据时重新加载 tableView。

    因为 alamofire 会执行 aschronus 调用。

    func Call_bitcoin(){
              let url = "https://api.coinbase.com/v2/prices/BTC-USD/buy"         
    Alamofire.request(url).responseObject
    { (response: DataResponse<Amount>) in 
          let mount = response.result.value                    
         let am = mount?.data print(am)
       tblView.reloadData()
     } 
    }
    

    当前您将空数组传递给 numberOfRowInSection,因此您需要传递计数如下

     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 10 //because your am.count empty
        } 
    

    【讨论】:

    • 我什至还没有在我的 tableView 中使用来自调用的数据。我正在使用测试字符串 'cell.textLabel?.text = "test"` '
    • 是的,但是您将空数组传递给 numberRowInSection ...否则传递硬编码整数。
    • 谢谢!对不起,我选择了上面的答案。他只是在这里没有那么多的代表!我相信你明白:)
    • 无效。我先给出了答案,然后我的答案值得接受。代表在这里无关紧要。
    猜你喜欢
    • 2013-02-24
    • 2013-01-26
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    相关资源
    最近更新 更多