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.