iOS无法从json到TableViewCell获取图像

时间:2015-11-10 02:27:39

标签: ios swift imageurl

我想从互联网链接中的json获取图像URL并在TableViewCell上显示它,但只能成功加载2标签上的文本。

这是我的代码:

MoviesCellController.swift:

import UIKit

class MoviesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var movies: [NSDictionary]!

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let url = NSURL(string: "https://coderschool-movies.herokuapp.com/movies?api_key=xja087zcvxljadsflh214")!
        let request = NSURLRequest(URL: url)

        NSURLConnection.sendAsynchronousRequest(request,
            queue: NSOperationQueue.mainQueue()) { (response, data, error) -> Void in

                let json = try? NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
                if let json = json {
                    self.movies = json!["movies"] as? [NSDictionary]
                    self.tableView.reloadData()
                }
        }
        tableView.dataSource = self
        tableView.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let movies = movies {
            return movies.count
        } else {
            return 0
        }
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("MovieCell", forIndexPath: indexPath) as! MovieCell

        let movie = movies![indexPath.row]

        cell.titleLabel.text = movie["title"] as? String
        cell.synopsisLabel.text = movie["synopsis"] as? String

        let url = NSURL(string: movie.valueForKeyPath("posters.thumbnail") as! String)!
        cell.posterView.setImageWithURL(url)

        return cell
    }

}

MovieCell.swift:

import UIKit

class MovieCell: UITableViewCell {

    @IBOutlet var titleLabel: UILabel!
    @IBOutlet var synopsisLabel: UILabel!    
    @IBOutlet var posterView: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

My MainStoryBoard

enter image description here

当我运行模拟器时,它只显示这样,不会在任何地方显示图像。

我的Info.plist文件:

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:2)

也许您没有在Info.plist文件中的NSAppTransportSecurity字典下将NSAllowsArbitraryLoads键设置为YES。

enter image description here

查看此帖子了解更多详情:Transport security has blocked a cleartext HTTP

答案 1 :(得分:0)

嗯。

假设您使用“setImageWithURL”的库正在运行,我会检查posterView插座是否正确连接,并且该URL实际上正在被解析。

答案 2 :(得分:0)

使用PINRemoteImage pods.just导入它并在cellForRowAtIndexPath

中编写以下代码

custumcell.profilePic?.pin_setImageFromURL(NSURL(string:profileImageUrl!),placeholderImage:UIImage(),processorKey:“Ball”,处理器:                         {(结果,费用) - > UIImage的!在                         返回result.image                         },完成:{(结果) - >无效}}