-1

please I need help I am new in swift and programming. I could not figure out how I can get my data in the table view cells.

// 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"}]

this is my class

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"]
    }


}

this is my function

    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)")                    
            }
        }

    }

here were I call the function

 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)
}

I am having error when declaring cell for each row.

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
    }

if any body can help I think I have to use for loop but how and were exactly I have to use it. I really

sara
  • 1
  • 4

1 Answers1

-1

I HAVE SOLVED THE ISSUE ... I HAVE ADD FOR LOOP IN THE CELL FOR ROW

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
    }
sara
  • 1
  • 4