Alamofire和Objectmapper将数组数据添加到TableView

时间:2018-08-11 12:27:28

标签: ios swift uitableview alamofire objectmapper

请帮助我,我是Swift和Programming的新手。我不知道如何在表格视图单元格中获取数据。

// 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循环,但是我必须以及如何使用它。我真的

1 个答案:

答案 0 :(得分:-1)

我已经解决了这个问题...我已经在行中添加了循环

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
    }