每个人,在查找有关如何为我的Movie DB应用程序实现搜索过滤器的文档时都遇到困难,因为到目前为止我的代码都无法附加,因此我设法将数据显示在表格视图中
标题和图像我现在需要做的是能够使用搜索栏过滤电影,然后在单击一行时转到新的View控制器,该控制器显示有关电影的扩展信息,然后单击“书签”按钮将其保存到核心数据中,然后出现在书签屏幕上,以供用户稍后查看。
预先感谢
我的代码在这里:
import UIKit
import AFNetworking
class ViewController: UIViewController, UITableViewDataSource,
UITableViewDelegate, UISearchBarDelegate {
var movies: [NSDictionary]?
@IBOutlet weak var txtSearch: UITextField!
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
// Do any additional setup after loading the view, typically from a nib.
fetchMovies()
searchBar()
}
func searchBar(){
let searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width:
self.view.frame.width, height: 50))
searchBar.delegate = self
searchBar.showsScopeBar = true
searchBar.tintColor = UIColor.lightGray
searchBar.scopeButtonTitles = ["Now Playing"]
self.tableView.tableHeaderView = searchBar
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
if searchText == ""{
fetchMovies()
}else{
if searchBar.selectedScopeButtonIndex == 0 {
movies = movies?.filter({ (movies) -> Bool in
return movies.title.lowercased()
})
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return movies?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MovieTableViewCell
let movie = movies![indexPath.row]
let title = movie["title"] as! String
let posterPath = movie["poster_path"] as! String
let baseUrl = "https://image.tmdb.org/t/p/w300/"
let imageUrl = NSURL(string: baseUrl + posterPath)
cell.lblTitle.text = title
cell.imgMovie.setImageWith(imageUrl! as URL)
print("row \(indexPath.row)")
return cell
}
func fetchMovies(){
let apiKey = "2c0a8efe934c162f5535ff33303e70bd"
let url = NSURL(string: "https://api.themoviedb.org/3/movie/now_playing?api_key=\(apiKey)")
let request = URLRequest(url: url! as URL, cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 10)
let session = URLSession(
configuration: URLSessionConfiguration.default, delegate: nil, delegateQueue: OperationQueue.main
)
let task: URLSessionDataTask = session.dataTask(with: request, completionHandler:{(dataOrNil, repsonse, error) in
if let data = dataOrNil {
if let responseDictionary = try! JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary{print("response: \(responseDictionary)")
self.movies = responseDictionary["results"] as? [NSDictionary]
self.tableView.reloadData()
}
}
})
task.resume()
}
@IBAction func btnBookmark(_ sender: UIButton) {
}
}