gpt4 book ai didi

iOS 无法从 json 获取图像到 TableViewCell

转载 作者:行者123 更新时间:2023-11-28 13:02:12 24 4
gpt4 key购买 nike

我想从互联网链接中的 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
}

}

我的主 Storyboard

enter image description here

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

我的 Info.plist 文件:

enter image description here

enter image description here

最佳答案

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

enter image description here

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

关于iOS 无法从 json 获取图像到 TableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33621852/

24 4 0
文章推荐: javascript -
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com