Advertisement
afeyajahin

Untitled

Nov 21st, 2021
1,333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.50 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //  Tipster
  4. //
  5. //  Created by AJM on 18/11/21.
  6. //
  7.  
  8. import UIKit
  9. import AlamofireImage
  10.  
  11. class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
  12.     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  13.         return movies.count
  14.     }
  15.    
  16.     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  17.         let cell = tableView.dequeueReusableCell(withIdentifier: "MovieCell") as! MovieCell
  18.         let movie = movies[indexPath.row]
  19.         let title = movie["title"] as! String
  20.         let synopsis = movie["overview"] as! String
  21.         cell.titleLabel.text = title
  22.         cell.synopsisLabel.text = synopsis
  23.        
  24.         let baseUrl = "https://image.tmdb.org/t/p/w185"
  25.         let posterPath = movie["poster_path"] as! String
  26.         let posterUrl = URL(string: baseUrl + posterPath)
  27.        
  28.         cell.posterView.af.setImage(withURL: posterUrl!)
  29.        
  30.         return cell
  31.            
  32.        
  33.     }
  34.     @IBOutlet weak var tableView: UITableView!
  35.     var movies = [[String:Any]]()
  36.  
  37.     override func viewDidLoad() {
  38.         super.viewDidLoad()
  39.        
  40.         tableView.dataSource = self
  41.         tableView.delegate = self
  42.         // Do any additional setup after loading the view.\
  43.         let url = URL(string: "https://api.themoviedb.org/3/movie/now_playing?api_key=a07e22bc18f5cb106bfe4cc1f83ad8ed")!
  44.         let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 10)
  45.         let session = URLSession(configuration: .default, delegate: nil, delegateQueue: OperationQueue.main)
  46.         let task = session.dataTask(with: request) { (data, response, error) in
  47.              // This will run when the network request returns
  48.              if let error = error {
  49.                     print(error.localizedDescription)
  50.              } else if let data = data {
  51.                     let dataDictionary = try! JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
  52.                    
  53.                     self.movies = dataDictionary["results"] as! [[String:Any]]
  54.                     self.tableView.reloadData()
  55.                     print(dataDictionary)
  56.                     // TODO: Get the array of movies
  57.                     // TODO: Store the movies in a property to use elsewhere
  58.                     // TODO: Reload your table view data
  59.  
  60.              }
  61.         }
  62.         task.resume()
  63.     }
  64.  
  65.  
  66. }
  67.  
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement