2016-05-02 16 views
0

私はParseデータベースからデータを取り出してテーブルビューを作成するiOSアプリケーションで作業しています。データをデータベースから取得してログに出力することはできますが、データを使用して表のセルにデータを挿入しようとすると、機能しません。テーブルは毎回空であり、私は理由を理解できません。何かご意見は?Parseデータベースでテーブルを作成

import UIKit 
import Parse 

var promoNum = -1 

class TableViewController: UITableViewController, CLLocationManagerDelegate { 

    var titles = [String]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     var getPromosQuery = PFQuery(className: "Promotions") 
     getPromosQuery.whereKey("zip", equalTo: "85281") 
     getPromosQuery.findObjectsInBackgroundWithBlock { (objects, error) in 
      if let objects = objects { 

       for object in objects { 

        self.titles.append(object["title"] as! String) 

       } 
      } 
     } 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    // MARK: - Table view data source 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of rows 
     return titles.count 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let promoCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! Cell 

     promoCell.promotionTitle.text = titles[indexPath.row] 

     return promoCell 
    } 

    override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? { 
     promoNum = indexPath.row 
     return indexPath 
    } 

} 

答えて

0

修正済み! self.titles.append(object["title"] as! String)の後にself.tableView.reloadData()を追加するだけでした。

+0

しかし、メインスレッドで 'reloadData'を呼び出していることを確認する必要があります。バックグラウンドスレッドでUI呼び出しをしないでください。また、 'reloadData'の呼び出しが、内部ではなく' for'ループ全体の後にあることを確認してください。 – rmaddy

関連する問題