gpt4 book ai didi

ios - 添加元素到搜索历史记录?

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

我有一个模型 - 电影。和两个 Controller - 第一个用于按标题搜索电影,第二个 - 用于显示带有海报、标题和年份的结果。现在我需要在我的第三个 Controller 上创建一些历史搜索(searchHistoryController - TableView)显示所有电影,当我点击带有电影标题的单元格时显示电影信息。我怎样才能 build 它?我尝试在我的模型中创建数组。并在其中写入结果,但每次我使用搜索时,它都会重写数组,而不是添加新元素。也许使用 Realm 需要一些帮助:)

Movie.swift

import Foundation
import UIKit
import Alamofire
import AlamofireImage


protocol MovieDelegate {
func updateMovieInfo()
}

class Movie {
private let omdbUrl = "http://www.omdbapi.com/?"
var title: String?
var filmYear: String?
var poster: String?

var delegete: MovieDelegate!

var historyMovie = [Movie]()



func getMovieInfo(title: String, completion: @escaping ()->()){
let params = ["t": title]
Alamofire.request(omdbUrl, method: .get, parameters: params).validate(statusCode: 200..<300).validate(contentType: ["application/json"]).responseJSON { (response) in
switch response.result {
case .success(let JSON):
let response = JSON as! NSDictionary
let status = response["Response"] as! String
if status == "True" {
self.title = (response["Title"] as! String)
self.filmYear = (response["Year"] as! String)
self.poster = (response["Year"] as! String)
// self.delegete.updateMovieInfo()
completion()
} else {
self.title = (response["Error"] as! String)
completion()
}
case .failure(let error):
print (error)

}
}

}

}

SearchVC

import UIKit
class SearchViewController: UIViewController {
var movie = Movie()
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
@IBOutlet weak var searchTextField: UITextField!
@IBOutlet weak var searchButton: UIButton!
@IBAction func searchButtonTapped(_ sender: UIButton) {
activityIndicator.startAnimating()
DispatchQueue.main.async {
self.movie.getMovieInfo(title: self.searchTextField.text!, completion: {
self.activityIndicator.stopAnimating()
self.performSegue(withIdentifier: "movieInfo", sender: self)
})
}
}

override func viewDidLoad() {
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let secondVC = segue.destination as! DetailInfoViewController
secondVC.movieTitle = movie.title!


}

}

DetailVC

class DetailInfoViewController: UIViewController, MovieDelegate {

@IBAction func showHistory(_ sender: UIButton) {
performSegue(withIdentifier: "showHistory", sender: self)
}
@IBOutlet weak var posterImageView: UIImageView!
@IBOutlet weak var filmNameLabel: UILabel!
@IBOutlet weak var filmYearLabel: UILabel!
var movie = Movie()
var movieTitle = ""

override func viewDidLoad() {
super.viewDidLoad()
self.movie.getMovieInfo(title: movieTitle ) {
self.updateMovieInfo()
}
self.movie.delegete = self
}

func updateMovieInfo() {
getPoster(link: movie.poster)
filmNameLabel.text = movie.title
filmYearLabel.text = movie.filmYear
}

func getPoster(link: String?) {
if link != nil {
guard let url = URL(string: link!) else { return }
DispatchQueue.global().async {
if let data = try? Data(contentsOf: url) {
DispatchQueue.main.async {
self.posterImageView.image = UIImage(data: data)
}
}
} } else {
self.posterImageView.image = #imageLiteral(resourceName: "Image")
}

}
}

最佳答案

首先,movieHistory 不应该是 Movie 类的一部分,而应该是 SearchViewController 类的一部分。其次,除非您想保留数据,否则不需要 Realm。

点击搜索按钮后,只需将 SearchViewController 中的电影保存到数组中,并将其发送到 segue 中的其他 View Controller 。就像这样

@IBAction func searchButtonTapped(_ sender: UIButton) {
activityIndicator.startAnimating()
DispatchQueue.main.async {
self.movie.getMovieInfo(title: self.searchTextField.text!, completion: {
self.activityIndicator.stopAnimating()
movieHistory.append(movie)
self.performSegue(withIdentifier: "movieInfo", sender: movieHistory)
})
}
}

另外,修改prepare(for segue:...)如下:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let secondVC = segue.destination as! DetailInfoViewController
secondVC.movieTitle = movie.title!
secondVC.movieHistory = movieHistory
}

详细来说,VC 还重写prepare(for segue:...) 并将 movieHistory 发送到 searchHistoryController,与之前的 VC 中的操作方式相同。

关于ios - 添加元素到搜索历史记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43524954/

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