【发布时间】:2020-09-02 19:41:26
【问题描述】:
我正在使用一个返回 JSON 作为响应的端点。问题是响应 json 对我来说是一个庞大的数据。从那我想显示所有 Surah(englishName) 名称以显示在 tableview 中。作为 iOS 开发的新手,我尽了最大的努力。请看看我的 sn-p,让我知道我哪里做错了。
ViewController 代码:
var surahName = [Surah]()
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK: JSON parse
func parseJSON() {
let url = URL(string: "https://api.alquran.cloud/v1/quran/ar.alafasy")
guard url != nil else{
print("URL Founr Nill")
return
}
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error == nil && data != nil{
do{
self.surahName = try JSONDecoder().decode([Surah].self, from: data!)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}catch{
print(error)
}
}
}.resume()
}
//MARK: Tableview delegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return surahName.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:QuranAudioCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! QuranAudioCell
let arrdata = surahName[indexPath.section].data.surahs
cell.nameLbl.text = arrdata[indexPath.row].englishName
return cell
}
问题是它没有在 tablview 中打印任何内容。
【问题讨论】: