我有一个包含这些数据的JSON表,我想获取标题+ oreview,然后将其显示在我的应用程序中,但是在这种情况下我被阻止了,我不知道如何进行操作,想法?。 我根据要求使用Alamofire。
我的标签json:
{"results":
[{"title":"The Predator",
"poster_path":"\/wMq9kQXTeQCHUZOG4fAe5cAxyUA.jpg"
"overview":"From the outer reaches of space to the small-town streets of suburbia, the hunt comes home. Now, the universe’s most lethal hunters are stronger, smarter and deadlier than ever before, having genetically upgraded themselves with DNA from other species. When a young boy accidentally triggers their return to Earth, only a ragtag crew of ex-soldiers and a disgruntled science teacher can prevent the end of the human race.",
"release_date":"2018-09-13"}
我的班级Movie.swift:
import UIKit
class Movie {
var title: String
var oreview: String
public init(title: String, oreview: String) {
self.title = title
self.oreview = oreview
}
public init?(json: [String: Any]) {
guard
let title = json["title"] as? String,
let oreview = json["oreview"] as? String else {
return nil
}
self.title = title
self.oreview = oreview
}
}
我的MovieTableVieWCell.swift:
import UIKit
import Alamofire
class MovieTableViewCell: UITableViewCell {
@IBOutlet var posterView: UIImageView!
@IBOutlet var title: UILabel!
@IBOutlet var oreview: UILabel!
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
}
@IBAction func DescriptionButton(_ sender: UIButton) {
}
}
extension MovieTableViewCell {
func draw(movie: Movie) {
self.title.text = "\(movie.title)"
self.oreview.text = "\(movie.oreview)"
}
}
我的MovieViewController.swift:
import UIKit
import Alamofire
import SwiftyJSON
class MovieViewController: UIViewController {
@IBOutlet var tableMovie: UITableView!
var movie: [Movie]?
override func viewDidLoad() {
super.viewDidLoad()
self.tableMovie.register(UINib(nibName: "MovieTableViewCell", bundle: nil), forCellReuseIdentifier: "Movie")
self.tableMovie.delegate = self
self.tableMovie.dataSource = self
// Do any additional setup after loading the view.
self.request()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public func request() {
let token = Token()
Alamofire.request("https://api.themoviedb.org/3/movie/upcoming?api_key=\(token.getToken())").responseJSON { response in
guard
let json = response.result.value as? [String: Any]
else {
return
}
print(json)
self.tableMovie.reloadData()
}
}
}
extension Movie: CustomStringConvertible {
var description: String {
let fields = [
"title: \(self.title)",
"oreview: \(self.oreview)",
]
return fields.joined(separator: ",")
}
}
extension MovieViewController: UITableViewDelegate {
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.movie?.count ?? 0
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Movie", for: indexPath) as! MovieTableViewCell
if let mov = self.movie?[indexPath.row] {
cell.draw(movie: mov)
}
return cell
}
}
extension MovieViewController: UITableViewDataSource {
}
答案 0 :(得分:0)
例如,将电影数据保存在数组中,使数据源返回包含array.count
元素的1部分。然后,表格视图将自动询问您将从数组中给出的元素。从请求中获取结果时,让表视图重新加载数据。
答案 1 :(得分:0)
您没有在电影中设置任何值。
首先,您需要解析响应并提取作为对象数组的结果
guard let json = response.result.value as? [String: Any], let results = json["results"] as? [[String: Any]] else {
return
}
第二,您需要将对象数组转换为电影数组并进行设置
self.movie = results.map { Movie(json: $0) }
第三,重新加载TableView
self.tableMovie.reloadData()
答案 2 :(得分:0)
首先,将您的json作为代码发布:
{"results":[
{
"title":"The Predator",
"poster_path":"/wMq9kQXTeQCHUZOG4fAe5cAxyUA.jpg",
"overview":"From the outer reaches of space to the small-town streets of suburbia, the hunt comes home. Now, the universe’s most lethal hunters are stronger, smarter and deadlier than ever before, having genetically upgraded themselves with DNA from other species. When a young boy accidentally triggers their return to Earth, only a ragtag crew of ex-soldiers and a disgruntled science teacher can prevent the end of the human race.",
"release_date":"2018-09-13"
}
]
}
第二,您的json已损坏。您在“ .... jpg”和“总览”之间没有逗号。您没有关闭方括号和大括号。你从哪里得到这个json的?
第三,您无法正确解析json,而且您期望将密钥称为"oreview"
,而在json中将其命名为"overview"
。
这是您应该如何做:
修改电影课:
class Movie {
var title: String
var overview: String
public init(title: String, overview: String) {
self.title = title
self.overview = overview
}
class func buildFrom(json: [String: Any]) -> Movie? {
let results = json["results"] as! [[String : Any]]
guard let title = results[0]["title"] as? String, let overview = results[0]["overview"] as? String else { return nil }
return Movie(title: title, overview: overview)
}
}
现在从自定义单元中删除所有代码:
class MovieTableViewCell: UITableViewCell {
@IBOutlet var posterView: UIImageView!
@IBOutlet var title: UILabel!
@IBOutlet var overview: UILabel!
}
现在,让我们转到您的viewController:
class MovieViewController: UIViewController {
@IBOutlet var tableMovie: UITableView!
var movie: Movie?
override func viewDidLoad() {
super.viewDidLoad()
self.tableMovie.register(UINib(nibName: "MovieTableViewCell", bundle: nil), forCellReuseIdentifier: "Movie")
self.tableMovie.delegate = self
self.tableMovie.dataSource = self
// Do any additional setup after loading the view.
self.request()
}
func request() {
let token = Token()
Alamofire.request("https://api.themoviedb.org/3/movie/upcoming?api_key=\(token.getToken())").responseJSON { response in
guard let json = response.result.value as? [String : Any] else { return }
self.movie = Movie.buildFrom(json: json)
self.tableMovie.reloadData()
}
}
}
//UITableView Datasource & Delegate methods
extension MovieViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.movie != nil {
return 1
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Movie", for: indexPath) as! MovieTableViewCell
cell.title = self.movie.title
cell.overview = self.movie.overview
return cell
}
}
这应该做到。如果您错误地粘贴了json并且它实际上是其中包含多于1部电影的json,那么将需要对代码进行一些更改。让我知道,我将更改答案。另外,不要忘记在问题中更新您的json。祝你好运!