所以我有一個解析一些比特幣價格數據的視圖控制器。該函數成功響應並解析數據,但我似乎無法讓它顯示在tableView中。tableView單元格不顯示數據,有什麼問題? (Swift 3)
我已經通過一個測試單元測試了網點和身份,其中確實是的工作。
我在做什麼錯?
代碼:`
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"]
}
}
class ViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource {
var mount = [String]()
var am = [String]()
@IBOutlet var tableView: UITableView!
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
self.tableView.reloadData()
return
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.delegate = self
tableView.dataSource = self
Call_bitcoin()
}
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).")
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
print(am)
cell.textLabel?.text = am[indexPath.row]
return cell
}
}
`
你設置了'tableView.dataSource = self'和'tableView.delegate = self'嗎? – Legonaftik
如果您的單元格不可見可能'am.count'返回0 –