2017-04-20 80 views
-1

我有一個模型 - 電影。 和兩個控制器 - 第一個用於按標題搜索電影,第二個 - 用於顯示帶有海報,標題和年份的結果。 現在我需要在我的第三個控制器 (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") 
     } 

    } 
} 

回答

0

首先,movieHistory不應該是Movie類的一部分,而應該是SearchViewController類的一部分。其次,除非你想堅持你的數據,否則你不需要Realm。

只要將搜索按鈕點擊並將其發送到您的其他視圖控制器中,只需將SearchViewController中的電影保存到數組中即可。像這樣

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

此外,修改準備(對於賽格瑞:...)這樣的:

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

在detailVC覆蓋準備(對於賽格瑞:...),以及發送給movieHistory searchHistoryController與之前的VC完成的方式相同。

+0

thanx很多!!!! – vo0ne