【发布时间】:2021-04-24 19:42:43
【问题描述】:
我正在玩https://jsonplaceholder.typicode.com,我正在尝试创建一个专辑列表。我尝试关注the example here,但专辑标题列表未显示在 TableView 上。我错过了什么?
class AlbumTableVC: UITableViewController {
var albums = [Album]()
override func viewDidLoad() {
super.viewDidLoad()
fetchAlbums()
}
func fetchAlbums() {
AF.request("https://jsonplaceholder.typicode.com/albums").responseJSON { (response) in
switch response.result {
case .success(let value):
let json = JSON(value)
for value in json.arrayValue {
let id = value.dictionaryValue["id"]!.int
let userId = value.dictionaryValue["userId"]!.int
let title = value.dictionaryValue["title"]!.string
let album = Album(id: id, userId: userId, title: title)
self.albums.append(album)
}
case .failure(let error):
print(error)
}
}
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return albums.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "AlbumCell", for: indexPath)
let album = albums[indexPath.row]
cell.textLabel?.text = album.title
return cell
}}
【问题讨论】:
标签: swift uitableview swifty-json jsonplaceholder