Ios 如何将图像解析为Tableview单元格?
导入UIKit 进口阿拉莫菲尔 导入快捷JSON 类主目录:UIViewController、UITableViewDelegate、UITableViewDataSource{Ios 如何将图像解析为Tableview单元格?,ios,swift,uitableview,uiimageview,alamofire,Ios,Swift,Uitableview,Uiimageview,Alamofire,导入UIKit 进口阿拉莫菲尔 导入快捷JSON 类主目录:UIViewController、UITableViewDelegate、UITableViewDataSource{ var tableView: UITableView = UITableView() var arrRes = [[String:AnyObject]]() override func viewDidLoad() { super.viewDidLoad() tableView = UITableVi
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)
}
}
好的,上面是我的代码。它现在显示的是带有歌曲和艺术家名称的UITableView。我的目标是在手机左侧显示相册艺术。有没有想过如何通过编程实现这一点
我曾尝试创建一个imageview并在单元格中返回它,但该图像不会显示,甚至无法设置一个虚拟图像来查看是否是解析错误 您需要首先从dict参数“CoverPictureMe”中的URL下载图像,然后将该图像放入
单元格.imageView?.image
。
以下链接将帮助您实现此目标:
dict[“coverPictureMeme”]实际上是下载图像的链接,而不是图像本身吗?图像可能就是这样。但是,作为测试,我很难让它显示在带有纯蓝色背景的表视图单元格中。我又看了一眼api,PictureMeme可能就是真实的图像。我正在使用的函数是get api/songs/topratedIt,它是一个字符串(可能是链接),而不是图像: