Alamofire 和 Objectmapper 将数组数据添加到 tableview
Posted
技术标签:
【中文标题】Alamofire 和 Objectmapper 将数组数据添加到 tableview【英文标题】:Alamofire and Objectmapper adding array data to tableview 【发布时间】:2018-08-11 12:27:27 【问题描述】:我需要帮助,我是 swift 和编程的新手。我不知道如何在表格视图单元格中获取数据。
// this is my API
["title":"Taylor Swift","artist":"Taylor Swift","url":"https://www.amazon.com/Taylor-Swift/dp/B0014I4KH6","image":"https://images-na.ssl-images-amazon.com/images/I/61McsadO1OL.jpg","thumbnail_image":"https://i.imgur.com/K3KJ3w4h.jpg","title":"Fearless","artist":"Taylor Swift","url":"https://www.amazon.com/Fearless-Enhanced-Taylor-Swift/dp/B001EYGOEM","image":"https://images-na.ssl-images-amazon.com/images/I/51qmhXWZBxL.jpg","thumbnail_image":"https://i.imgur.com/K3KJ3w4h.jpg","title":"Speak Now","artist":"Taylor Swift","url":"https://www.amazon.com/Speak-Now-Taylor-Swift/dp/B003WTE886","image":"https://images-na.ssl-images-amazon.com/images/I/51vlGuX7%2BFL.jpg","thumbnail_image":"https://i.imgur.com/K3KJ3w4h.jpg","title":"Red","artist":"Taylor Swift","url":"https://www.amazon.com/Red-Taylor-Swift/dp/B008XNZMOU","image":"https://images-na.ssl-images-amazon.com/images/I/41j7-7yboXL.jpg","thumbnail_image":"https://i.imgur.com/K3KJ3w4h.jpg","title":"1989","artist":"Taylor Swift","url":"https://www.amazon.com/1989-Taylor-Swift/dp/B00MRHANNI","image":"https://images-na.ssl-images-amazon.com/images/I/717DWgRftmL._SX522_.jpg","thumbnail_image":"https://i.imgur.com/K3KJ3w4h.jpg"]
这是我的课
import Foundation
import ObjectMapper
class AlbumInfo: Mappable
//MARK: create Album title and artist name variable for each album
var albumTitle: String?
var artistName: String?
var urlToBuyALbum: String?
var albumImage: String?
var thumbnailImage: String?
required init?(map: Map)
func mapping(map: Map)
albumTitle <- map["title"]
artistName <- map["artist"]
urlToBuyALbum <- map["url"]
albumImage <- map["image"]
thumbnailImage <- map["thumbnail_image"]
这是我的功能
func getTaylorSwiftAlbumData (url: String)
Alamofire.request(url, method : .get).responseJSON
response in
if response.result.isSuccess
print("Success! Got the album data")
self.albumInfoArray = Mapper<AlbumInfo>().mapArray(JSONObject: response.result.value!)!
self.tableView.reloadData()
else
print("Error \(response.result.error)")
我在这里调用函数
let taylorAlbumURL = "https://rallycoding.herokuapp.com/api/music_albums"
var albumInfoArray : [AlbumInfo] = [AlbumInfo]()
override func viewDidLoad()
super.viewDidLoad()
tableView.register(UINib(nibName: "CustomCell", bundle: nil) , forCellReuseIdentifier: "customCustomCell")
getTaylorSwiftAlbumData (url: taylorAlbumURL)
为每一行声明单元格时出现错误。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "customCustomCell", for: indexPath) as! CustomTableCell
//
let convertThumbImagetoURL = URL(string: albumInfoArray[indexPath.row].thumbnailImage!)
cell.albumTitleLabel.text = albumInfoArray[indexPath.row].albumTitle
cell.artistNameLabel.text = albumInfoArray[indexPath.row].artistName
cell.thumbnailImage.af_setImage(withURL: convertThumbImagetoURL!)
return cell
如果有任何机构可以提供帮助,我认为我必须使用 for 循环,但我必须如何以及究竟如何使用它。我真的
【问题讨论】:
你设置了委托和数据源吗?还是 UITableViewController 它是 UITableViewController。非常感谢你的回复。解决了。 【参考方案1】:我已经解决了这个问题……我已经在 ROW 的单元格中添加了 FOR 循环
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "customCustomCell", for: indexPath) as! CustomTableCell
if albumInfoArray.count > 0
let convertThumbImagetoURL = URL(string: albumInfoArray[indexPath.row].thumbnailImage!)
cell.albumTitleLabel.text = albumInfoArray[indexPath.row].albumTitle
cell.artistNameLabel.text = albumInfoArray[indexPath.row].artistName
cell.thumbnailImage.af_setImage(withURL: convertThumbImagetoURL!)
ELSE
PRINT (" ERROR ")
return cell
【讨论】:
以上是关于Alamofire 和 Objectmapper 将数组数据添加到 tableview的主要内容,如果未能解决你的问题,请参考以下文章
使用 ObjectMapper 和 Alamofire 时对象始终为零
如何通过 objectMapper 和 Alamofire 发布嵌套的 json?
Alamofire 和 Objectmapper 将数组数据添加到 tableview
Alamofire,Objectmapper,Realm:嵌套对象