gpt4 book ai didi

swift - 如何使用 Alamofire 请求在 tableView 中显示我的数据

转载 作者:行者123 更新时间:2023-11-30 11:08:46 25 4
gpt4 key购买 nike

我有一个包含这些数据的 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"}

我的类(class)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 {

}

最佳答案

您没有在电影中设置任何值。

首先,您需要解析响应并提取结果,这是一个对象数组

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()

关于swift - 如何使用 Alamofire 请求在 tableView 中显示我的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52414739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com