【问题标题】:Alamofire and Objectmapper adding array data to tableviewAlamofire 和 Objectmapper 将数组数据添加到 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。非常感谢你的回复。解决了。​​

标签: ios swift uitableview alamofire objectmapper


【解决方案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
    }

【讨论】:

    猜你喜欢
    • 2018-08-25
    • 2020-11-27
    • 1970-01-01
    • 2020-04-07
    • 2018-12-23
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多