1

Everyone, I'm having difficulties finding documentation on how to implement a Search filter for My Movie DB app ill attach my code so far below I've managed to get the data to show up in the table view

The title and Image What I need to do now is being able to filter the movies with a search bar then when clicking on a row it goes to a new View controller displaying expanded information about the movie then a button to "bookmark" save it to core data then appear on a bookmark screen for the user to view later.

Thanks in Advance

My code is here:

    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) {
    }
    }
Community
  • 1
  • 1
Harvista
  • 31
  • 6

0 Answers0