2017-02-02 11 views
0

Swift 3を使用してXcodeでjSONデータを表示しようとすると、ビルドエラーが発生します。私のコードの一部をこのページにコピーします。 。UITable表示されません。値の種類 'FilmsAPITableViewCell'にメンバー 'movieTitle'がありません

私はこのサイトで同様の質問を見つけましたが、回答は役に立たないようです。カスタムセルクラスは、そのラベルのコンセントを持っていないので、ここで

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 { 


     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath) as! FilmsAPITableViewCell   
     // 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("740c94c51891c02b64d6c78840b478fe0b02fe2c", forHTTPHeaderField: "X-API-KEY") 
     request.setValue("Basic YmhlZW0uZW5nckBnbWFpbC5jb206YmgzM20=", 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.tableView.reloadData() 
          }) 

         } 
        } 

       } 
       print("Hello") 
       self.tableView.reloadData() 
       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. 
    } 



} 

もFilmsAPITableViewCell.swift

import UIKit 

輸入のUIKit

class FilmsAPITableViewCell: UITableViewCell { 
    @IBOutlet weak var movieTitle: UILabel! 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 

    override func setSelected(_ selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 

} 
+0

:ここ

も、それが働いて取得するように私が編集したあなたのコードです。 – vadian

+0

@vadianはいそうです。フィードバックをお寄せいただきありがとうございます。私がチェックすべき他の何か? –

+0

'FilmsAPITableViewCell.swift'を表示できますか? – Losiowaty

答えて

0

から出力されmovieTitleがnilである理由はあります。 .xib(またはストーリーボード)にUILabelを作成し、独自のセルクラス内にコンセント接続を作成する必要があります。

完了ブロックにはself.tableView.reloadData()も存在しないようです。 print("Hello")行の直後に追加してください。

P.S.リロードをメインキューにディスパッチすることを忘れないでください。ほとんどの場合、Interface BuilderでカスタムセルのクラスはFilmsAPITableViewCell` `に設定されていない

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.dataSource = self 
    tableView.delegate = self 

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

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

    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 
     } 



     var json:Any? 

     do 
     { 
      if let existingData = data { 
       json = try JSONSerialization.jsonObject(with: existingData, options: []) 
      } 

      // Prasing JSON 
      if let parsedData = json as? [[String:Any]] { 
       for dict in parsedData { 
        if let title = dict["title"] as? String { 
         self.FilmArray.append(title) 
        } 
       } 

       OperationQueue.main.addOperation({ 
        self.tableView.reloadData() 
       }) 
      } 
     } 
     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() 
} 
+0

あなたは正しいです。私は、あなたがデータのリロードについてコメントしただけです。私はすぐに私の答えを更新します。 – Prientus

+0

ありがとうございました!私は提案を追加したが、まだ動作していない。私は –

+0

の上に更新されたコードを掲示します@KendellRogers私はあなたがする必要があるもう2つの事があると思います。 weak tableViewプロパティは、.xib(またはストーリーボード)のtableViewへのコンセントのようには見えません。接続してみてください。正直言って、私はあなたもこれらの行が必要であるとは思わない: 'let tableView = UITableView(frame:view.bounds) view.addSubview(tableView) self.tableView = tableView' コンセントはあなたのために仕事をするはずです。 – Prientus

関連する問題