2017-02-02 2 views
0

私はこの問題でロードブロッキングを犯しました。私は指導のためにこれらのURL1URL2を試しましたが、成功しませんでした。 UITableViewにデータが設定されていないJSONを修正するにはどうすればよいですか? TableViewで無効にする配列を取得する際に問題が発生しています。あなたはFilmArrayコールtableview.reloadData()に最後のタイトルを入れた後JSONのUITableViewへの挿入を修正するにはどうすればよいですか?

class FilmsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 
     weak var tableView : UITableView! 
     var FilmArray = [String]() 

     let film_url = "https://www.testing.com/api/resources/films/1" 
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      return 1 
     } 
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
      // Getting the right element 
      //let films = FilmArray[indexPath.row] 


      // Instantiate a cell 
      //let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "moviecell") 
      let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FilmsAPITableViewCell 
      //  cell.movieTitle.text = FilmArray[indexPath.row] 
      // Adding the right informations 
      cell.movieTitle.text = FilmArray[indexPath.row] 
      // Returning the cell 
      return cell 
     } 
     // @IBOutlet weak var FilmsView: UITableView! 
    // weak var tableView : UITableView! 
    // var FilmArray = [String]() 
    // 
    // let film_url = "https://www.distribber.com/api/resources/films/1" 
    //  
     override func viewDidLoad() { 
      super.viewDidLoad() 

      let tableView = UITableView (frame:view.bounds) 
      view.addSubview(tableView) 
      self.tableView = tableView 

      tableView.dataSource = self 
      tableView.delegate = self 


    //  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    //   return 1 
    //  } 
    //   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    //    // Getting the right element 
    //    //let films = FilmArray[indexPath.row] 
    //     
    //     
    //    // Instantiate a cell 
    //    //let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "moviecell") 
    //    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FilmsAPITableViewCell 
    //    //  cell.movieTitle.text = FilmArray[indexPath.row] 
    //    // Adding the right informations 
    //    cell.movieTitle.text = FilmArray[indexPath.row] 
    //    // Returning the cell 
    //    return cell 
    //  } 
       // } 

      //} 



      let url:URL = URL(string: film_url)! 
      let session = URLSession.shared 

      let request = NSMutableURLRequest(url: url) 
      request.httpMethod = "GET" 
      request.setValue("masked", forHTTPHeaderField: "X-API-KEY") 
      request.setValue("masked=", forHTTPHeaderField: "Authorization") 
      request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData 
      let paramString = "" 


      //  for (key, value) in post_data 
      //  { 
      //   paramString = paramString + (key as! String) + "=" + (value as! String) + "&" 
      //  } 
      // 
      request.httpBody = paramString.data(using: String.Encoding.utf8) 

      let task = session.dataTask(with: request as URLRequest, completionHandler: { 
       (
       data, response, error) in 

       guard let _:Data = data, let _:URLResponse = response , error == nil else { 

        return 
       } 



       let json: Any? 

       do 
       { 
        json = try JSONSerialization.jsonObject(with: data!, options: []) 

        // Prasing JSON 
        var parsedData = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any] 
        print(parsedData) 
        if let FilmArray = parsedData["films"] as? NSArray { 
         for movieTitle in FilmArray{ 
          if let filmDict = movieTitle as? NSDictionary{ 
           if let film = filmDict.value(forKey: "title") { 
           self.FilmArray.append(film as! String) 
           } 

           //OperationQueue.main.addOperation({ 
            //self.FilmsView.reloadData() 
           // }) 

          } 
         } 

        } 
        print("Hello") 
        print(self.FilmArray) 
       } 
       catch 
       { 
        return 
       } 

       guard let server_response = json as? NSDictionary else 
       { 
        return 
       } 


       if let data_block = server_response["data"] as? NSDictionary 
       { 
        if let session_data = data_block["session"] as? String 
        { 
         // self.login_session = session_data 

         let preferences = UserDefaults.standard 
         preferences.set(session_data, forKey: "session") 

         // DispatchQueue.main.async(execute: self.LoginDone) 
        } 
       } 



      }) 

      task.resume() 

      // Do any additional setup after loading the view. 
     } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

["status": 1, "films": <__NSSingleObjectArrayI 0x7ae40320>(
{ 
    Stores =  (
       { 
      Assets =    (
           { 
        "asset_name" = Screener; 
        "asset_status" = Needed; 
       }, 
           { 
        "asset_name" = Feature; 
        "asset_status" = Needed; 
       }, 
           { 
        "asset_name" = Trailer; 
        "asset_status" = Needed; 
       }, 
           { 
        "asset_name" = Artwork; 
        "asset_status" = Needed; 
       }, 
           { 
        "asset_name" = "Closed Caption"; 
        "asset_status" = Needed; 
       }, 
           { 
        "asset_name" = Meta; 
        "asset_status" = Needed; 
       } 
      ); 
      Status = Declined; 
      "store_name" = "Netflix Watch <br> Instantly (SD)"; 
     } 
    ); 
    title = "The Red Tail"; 
} 
) 
] 
+0

データソースが変更されるたびにtableviewでreloadDateを呼び出す必要があります。 コード内で、すべてのムービータイトルの追加が完了したら、self.tableView.reloadData()を呼び出します。 – TheFuquan

+0

言うまでもなく、mainQueue – TheFuquan

答えて

0

は、ここに私のコードです。あなたはメインキューでそれを行う必要があります。そうしないと、予期しない結果が得られます。

関連する問題