0

I want to get image url from json in internet link and show it on the TableViewCell, but only load successfully only text on 2 label.

Here is my code:

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

When I run the simulator it only show like this, doesn't show the image anywhere.

my Info.plist file:

enter image description here

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Twitter khuong291
  • 11,328
  • 15
  • 80
  • 116

3 Answers3

2

Maybe you don't set NSAllowsArbitraryLoads key to YES under NSAppTransportSecurity dictionary in your Info.plist file.

enter image description here

Check this post for more detail: Transport security has blocked a cleartext HTTP

Community
  • 1
  • 1
Quang Hà
  • 4,613
  • 3
  • 24
  • 40
  • Maybe I have not added it yet, I check this link but where should I add it. http://stackoverflow.com/questions/31216758/how-can-i-add-nsapptransportsecurity-to-my-info-plist-file – Twitter khuong291 Nov 10 '15 at 03:42
  • Info.plist file, you can search in your project if you're new with Xcode – Quang Hà Nov 10 '15 at 03:44
0

Hmm.

Assuming that the library you are using to "setImageWithURL" is working, I would check that the posterView outlet is properly wired up, and that url is actually being parsed.

Brad The App Guy
  • 16,255
  • 2
  • 41
  • 60
0

use PINRemoteImage pods.just import it and write the below code in cellForRowAtIndexPath

custumcell.profilePic?.pin_setImageFromURL(NSURL(string: profileImageUrl!), placeholderImage: UIImage(), processorKey: "Ball", processor: { (result, cost) -> UIImage! in return result.image }, completion: { (result) -> Void in })

Amol Pokale
  • 173
  • 1
  • 4