简体   繁体   English

如何将图像解析为 Tableview 单元格?

[英]How would I parse a image into a Tableview cell?

import UIKit import Alamofire import SwiftyJSON导入 UIKit 导入 Alamofire 导入 SwiftyJSON

class Home: UIViewController, UITableViewDelegate,UITableViewDataSource{类主页: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.它现在显示的是带有歌曲和艺术家名称的 UITableView。 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.我试过创建一个图像视图并将它返回到一个单元格中,但图像不会显示甚至设置一个虚拟图像来查看它是否是错误的解析。

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 .您需要先从 dict 参数“coverPictureMime”中的 URL 下载图像,然后将该图像放入cell.imageView?.image Following link will help you to achieve this: Download image from URL以下链接将帮助您实现这一目标: 从 URL 下载图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM