2016-10-12 7 views
1
class ProductTableViewController: UITableViewController 
{ 
    var products = [Product]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     getData() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    override func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return products.count // zero 
    } 


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "ProductCell", for: indexPath) as! ProductCell 
     cell.userIdLabel?.text = "user id" + "$\(products[indexPath.row].userId)" 
     cell.idLabel?.text = "id" + "$\(products[indexPath.row].id)" 
     cell.titleLabel?.text = products[indexPath.row].title 
     cell.bodyLabel?.text = products[indexPath.row].body 
     return cell 
    } 

    func getData(){ 
     let url = URL(string: "https://jsonplaceholder.typicode.com/posts") 
     URLSession.shared.dataTask(with:url!, completionHandler: {(data, response, error) in 
      guard let data = data, error == nil else { return } 
      do { 
       let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [[String:Any]] 
       for post in json{ 
        let product = Product() 
        product.userId = post["userId"] as! Int 
        product.id = post["id"] as! Int 
        product.title = post["title"] as! String 
        product.body = post["body"] as! String 
        self.products.append(product) 
       } 
       //OperationQueue.main.addOperation({() -> Void in self.tableView.reloadData()}) 
      } catch let error as NSError { 
       print(error) 
      } 
//HERE!!! 
      OperationQueue.main.addOperation({() -> Void in self.tableView.reloadData()}) 
     }).resume() 
    } 
} 

最初に実行されると、func tableViewが実装され、0カウントが返されます。getData()はまだ実行されていません。もちろん、私のCellを返す第2のtableViewは実装されません。
そして今、私はOperationQueue.main.addOperation({() -> Void in self.tableView.reloadData()})
と私のtableViewをリロードしてみたが、エラーをキャッチ行うために、私のgetData()を解析されたものを見たい:tableView.reloadData()データを正しく再読み込みするには?

スレッド1:SIGABRT。

テーブルビューを正しく再ロードする方法を教えてください。

+0

を試してみてください?編集して質問を改善してください。 StackOverflowでもうまくいきます。 – Astoria

+0

実行中、実行しますか? –

+0

tableView.dequeueReusableCellは、tableViewが0のセルで始まっていれば 'nil'を返します。この場合、最初にUITableViewCellを初期化する必要があります。 – frzi

答えて

1

意味 "のtableView funcを実現します" はどのようなこの

func getData(){ 
    let url = URL(string: "https://jsonplaceholder.typicode.com/posts") 
    URLSession.shared.dataTask(with:url!, completionHandler: {(data, response, error) in 
     guard let data = data, error == nil else { return } 
     do { 
      let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [[String:Any]] 
      for post in json{ 
       let product = Product() 
       product.userId = post["userId"] as! Int 
       product.id = post["id"] as! Int 
       product.title = post["title"] as! String 
       product.body = post["body"] as! String 
       self.products.append(product) 
      } 
      // just reload here after finish appends 
      self.tableView.reloadData() 
     } catch let error as NSError { 
      print(error) 
     } 
    }).resume() 
} 
関連する問題