iOS 无法从 json 获取图像到 UITableViewCell
iOS can not get image from json to TableViewCell
我想从互联网 json 获取图像 url link 并将其显示在 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
}
}
我的 MainStoryBoard
当我 运行 模拟器时它只显示这样,不显示任何地方的图像。
我的Info.plist文件:
嗯。
假设您用于 "setImageWithURL" 的库正在运行,我将检查 posterView 插座是否正确连接,并且 url 是否正在被解析。
也许您没有在 Info.plist 文件的 NSAppTransportSecurity 字典下将 NSAllowsArbitraryLoads 键设置为 YES。
查看此 post 了解更多详情:
使用 PINRemoteImage pods.just 将其导入并在 cellForRowAtIndexPath
中写入以下代码
custumcell.profilePic?.pin_setImageFromURL(NSURL(string: profileImageUrl!), placeholderImage: UIImage(), processorKey: "Ball", processor:
{(结果,成本)-> UIImage!在
return result.image
}, 完成: { (result) -> Void in })
我想从互联网 json 获取图像 url link 并将其显示在 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
}
}
我的 MainStoryBoard
当我 运行 模拟器时它只显示这样,不显示任何地方的图像。
我的Info.plist文件:
嗯。
假设您用于 "setImageWithURL" 的库正在运行,我将检查 posterView 插座是否正确连接,并且 url 是否正在被解析。
也许您没有在 Info.plist 文件的 NSAppTransportSecurity 字典下将 NSAllowsArbitraryLoads 键设置为 YES。
查看此 post 了解更多详情:
使用 PINRemoteImage pods.just 将其导入并在 cellForRowAtIndexPath
中写入以下代码custumcell.profilePic?.pin_setImageFromURL(NSURL(string: profileImageUrl!), placeholderImage: UIImage(), processorKey: "Ball", processor: {(结果,成本)-> UIImage!在 return result.image }, 完成: { (result) -> Void in })