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

时间:2017-04-20 16:24:28

标签: ios swift swift3 realm

我有一个模特 - 电影。 和两个控制器 - 首先按标题搜索电影,第二个 - 用于显示海报,标题和年份的结果。 现在我需要在我的第三个控制器上创建一些历史搜索 (searchHistoryController - TableView)显示所有电影,当我点击带有电影的标题显示电影信息的单元格时。 我该如何建造它? 我尝试在我的模型中创建数组。并在其中写入resutl,但每次当我使用搜索时它重写数组,而不是添加新元素。 也许使用领域 需要一些帮助:)

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")
        }

    }
}

1 个答案:

答案 0 :(得分:0)

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

点击搜索按钮后,只需将SearchViewController中的电影保存到数组中,然后将其发送到segue中的其他视图控制器。像这样

@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(对于segue:...),如下所示:

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

详细说明VC覆盖prepare(对于segue:...)并将movieHistory发送到searchHistoryController,就像在前一个VC中完成一样。