【问题标题】:How would I parse a image into a Tableview cell?如何将图像解析为 Tableview 单元格?
【发布时间】:2017-01-21 14:16:32
【问题描述】:

导入 UIKit 进口阿拉莫火 导入 SwiftyJSON

类首页: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)
}

}

好的,以上是我的代码。它现在显示的是带有歌曲和艺术家名称的 UITableView。我的目标是让它在单元格的左侧显示专辑封面。关于如何以编程方式执行此操作的任何想法?

我尝试创建一个图像视图并将其返回到一个单元格中,但图像不会显示,甚至设置一个虚拟图像以查看它是否是错误的解析。

【问题讨论】:

  • dict["coverPictureMime"]实际上不是下载图片的链接,而不是图片本身吗?
  • 嗯,实际上可能是图像的情况。但是,我无法将其显示在带有纯蓝色背景的表格视图单元格内作为测试。我又看了一下 api,pictureMime 可能是实际的图像。我正在使用vocadb.net/swagger/ui/index#,我使用的功能是get api/songs/toprated
  • 这是一个字符串(可能是链接),而不是图像:monosnap.com/file/4PVvQv1qPyCt686kyLMz1jaaiZzlIc

标签: ios swift uitableview uiimageview alamofire


【解决方案1】:

您需要先从 dict 参数“coverPictureMime”中的 URL 下载图像,然后将该图像放入 cell.imageView?.image。 以下链接将帮助您实现这一目标: Download image from URL

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多