-1

import UIKit import Alamofire import SwiftyJSON

class Home: UIViewController, UITableViewDelegate,UITableViewDataSource{

var tableView: UITableView = UITableView()
var arrRes = [[String:AnyObject]]()

override func viewDidLoad() {
    super.viewDidLoad()

    tableView = UITableView(frame: UIScreen.main.bounds, style: UITableViewStyle.plain)
    tableView.separatorStyle = UITableViewCellSeparatorStyle.none
    tableView.delegate   = self
    tableView.dataSource = self
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    self.view.addSubview(self.tableView)

    // sort for popular songs
    Alamofire.request("http://vocadb.net/api/songs/top-rated?filterBy=Popularity").responseJSON { (responseData) -> Void in
        if((responseData.result.value) != nil) {
            let swiftyJsonVar = JSON(responseData.result.value!)

            if let resData = swiftyJsonVar[].arrayObject {
                self.arrRes = resData as! [[String:AnyObject]]
            }
            if self.arrRes.count > 0 {
                self.tableView.reloadData()
            }
        }
    }
}



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "cell")
    var dict = arrRes[indexPath.row]
    cell.textLabel?.textColor = .white
    cell.detailTextLabel?.textColor = .white
    cell.textLabel?.text = dict["defaultName"] as? String // use default name for each song
    cell.detailTextLabel?.text = dict["artistString"] as? String // use artist string and the second line of text
    cell.imageView?.image = dict["coverPictureMime"] as? UIImage

    return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrRes.count

}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.contentView.backgroundColor = UIColor(red:0.16, green:0.16, blue:0.16, alpha:1.0)
}

}

Okay so above is my code. What it displays now is UITableView with names of songs and artist. My goal is to go it to display the album art on the left side of the cell. Any thoughts on how I could do this programmatically?

I've tried created a imageview and returning it inside a cell but the image wouldn't show even set a dummy image to see if it was the parsing that was wrong.

  • 1
    Isn't `dict["coverPictureMime"]` actually the *link* for downloading the image, and not the image itself? – Eric Aya Jan 21 '17 at 14:18
  • Well that actually may be the case with the image. However, I'm having trouble just getting it to display inside the table view cell with a solid blue background as a test . I took another look at the api and pictureMime might be the actual image. I'm using http://vocadb.net/swagger/ui/index#/ and the function im using is get api/songs/toprated – Dengekiko Jan 21 '17 at 14:23
  • It's a string (probably the link), not the image: https://monosnap.com/file/4PVvQv1qPyCt686kyLMz1jaaiZzlIc – Eric Aya Jan 21 '17 at 14:27

1 Answers1

0

You need to download the image first from the URL coming in your dict parameter "coverPictureMime" and then put that image in cell.imageView?.image. Following link will help you to achieve this: Download image from URL

Community
  • 1
  • 1
Ankit Saini
  • 284
  • 2
  • 14