gpt4 book ai didi

ios - tableView单元格不显示数据,有什么问题? ( swift 3)

转载 作者:行者123 更新时间:2023-11-30 12:24:58 25 4
gpt4 key购买 nike

所以我有一个 View Controller 正在解析一些比特币价格数据。该函数成功响应并解析数据,但我似乎无法让它显示在 tableView 中。

我已经通过测试单元测试了 socket 和身份,这确实有效。

我做错了什么?

代码:`

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
}

}

`

最佳答案

您想在 Call_bitcoin() 函数中添加元素,但您做错了。您已经创建了 var am = [String]()。因此您不需要使用 let am = mount?.data。您可以将数据添加到已创建的 am 变量中。您需要更改 Call_bitcoin() 函数中的一些代码行:

来自:

let mount = response.result.value
let am = mount?.data

致:

let mount = response.result.value
am.append(mount?.data) // This part should be set by your 'mount?.data' value type

关于ios - tableView单元格不显示数据,有什么问题? ( swift 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44330870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com