【发布时间】:2017-10-26 01:24:03
【问题描述】:
在我陈述我的问题之前,我想让大家知道我是 Swift 编码环境的新手,所以请原谅我缺乏知识。目前,我无法根据从 JSON URL 返回的数据使用 Alamofire 填充表格视图的单元格。当我在模拟器中运行应用程序时,数据会显示在控制台中,但应用程序会因 SIGABRT 错误而崩溃。作为参考,我没有使用带有 tableview 元素的 viewcontroller,而是使用 tableviewcontroller。到目前为止,这是我的代码:
import UIKit
import Alamofire
class TableViewController: UITableViewController {
var responseArray: NSArray = []
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request("https://rss.itunes.apple.com/api/v1/us/apple-music/top-songs/all/10/explicit.json").responseJSON { response in
if let json = response.result.value {
print(json)
self.responseArray = json as! NSArray
}
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return responseArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "top10", for: indexPath)
// Configure the cell...
let whichSong = responseArray[(indexPath as NSIndexPath).row]
let artistName = (whichSong as AnyObject)["artistName"] as? String
cell.textLabel?.text = artistName
return cell
}
【问题讨论】:
-
究竟是哪一行导致了错误?控制台中的完整错误消息是什么?
-
你使用的是 Storyboard 还是 XIB ??
-
我正在使用 Xcode 中提供的故事板。至于错误,似乎是来自self.responseArray = json as! NSArray。另外,json请求后是否需要重新加载数据?
-
你没有重新加载你的表
-
很遗憾,添加 reload 语句并没有解决问题
标签: json swift uitableview alamofire