gpt4 book ai didi

arrays - JSON 数组不会快速显示在 UITable 中

转载 作者:行者123 更新时间:2023-11-28 13:01:55 25 4
gpt4 key购买 nike

我正在尝试创建一个表格,该表格从在线 JSON 文件中填充数据。我目前在线拥有 JSON 文件并被调用到类中并转换为数组,但是当我尝试将我的文本设置为该数组数据时,没有任何显示。

cell.textLabel?.text = "\(locationName[indexPath.row])"

但是,如果我在数组中设置数据,它会起作用,但我需要数据来自 JSON 文件。

var array: double = ["1", "2", "3"]

这是我的表类代码和数组代码。任何帮助都会非常感谢

class SpotsDataClass: UITableViewController {

var locationName = [String]()

override func viewDidLoad() {
super.viewDidLoad()

Alamofire.request(.GET, "http://woamph.com/savedLocations.json")
.response { request, response, data, error in

if let data = data {

let json = JSON(data: data)

for locationLoop in json {
let usersJSON = locationLoop.1["user"].stringValue
self.locationName.append(usersJSON)
//let longitudeJSON = locationLoop.1["longitude"].doubleValue
//self.array2.append(longitudeJSON)
}

for i in 0 ..< self.locationName.count {
print("\(self.locationName[i])")
//print("\(self.array2[i])")
//self.locationNameLabel.text = "Location Name: \(self.array1[i])"
}

//self.titleLabel.text = "Spots Near You"
//self.locationNameLabel.text = "Location Name: \(spotData.locationName)"
//self.ratingLabel.text = "Rating: \(spotData.rating)"
//self.userLabel.text = "Posted By: \(spotData.user)"
//
}
}
}

//Mark: - UITableDataSource

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("spotTable", forIndexPath: indexPath) as! UITableViewCell

cell.textLabel?.text = "\(locationName[indexPath.row])"
return cell
}


}

最佳答案

Alamofire 请求异步执行。您需要明确告诉您的 TableView 在网络请求完成时重新加载,否则您将根据结果填充数组,但在加载远程数据时 TableView 已经加载。

所以,之后

for locationLoop in json {
let usersJSON = locationLoop.1["user"].stringValue
self.locationName.append(usersJSON)
}

添加:tableView.reloadData()

关于arrays - JSON 数组不会快速显示在 UITable 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33659447/

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