2015-11-04 99 views
0

我正在練習解析現在,我現在如何檢索數據,但問題是 當我嘗試在解析中保存對象時,我的第一個打印(postsArray)不顯示任何內容。這是我的代碼。之後我追加數據parse.com追加檢索數據

import UIKit 
import Parse 

class TableViewController: UITableViewController { 

var postsArray = [PFObject]() 
override func viewDidLoad() { 
    super.viewDidLoad() 




    fetchData() 
    print(postsArray) 
    tableView.reloadData() 


} 

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 postsArray.count 
} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("practiceCell", forIndexPath: indexPath) 

    // Configure the cell... 

    cell.textLabel!.text = postsArray[indexPath.row].objectForKey("text") as? String 
    return cell 



} 

func fetchData() { 



     //empty postArray 
     postsArray = [] 

     //bring data from parse 
     let query = PFQuery(className: "Practice") 

     query.orderByDescending("createdAt") 
     query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error) -> Void in 
      if error == nil && objects != nil{ 
       for object in objects! { 

        self.postsArray.append(object) 
        print(self.postsArray) 
       } 

      } 
     } 
    } 
} 

打印方法,顯示正確的數據,但在viewDidLoad中第一次打印()方法告訴我什麼。所以我的tableView也不顯示任何東西。

我讀解析文檔和搜索其他問題,這似乎沒有錯,它不工作。

你能給我一個建議嗎?

回答

0

查詢完成後,您需要self.tableView.reloadData()這將導致tableView再次委託它委託它應該有多少行,在返回數據後這將是準確的。或者,您可以使用一個PFQueryTableViewController,它將自行處理它的對象,並使您更容易管理。

+0

你是對的,它的工作原理。 –

+0

很高興能幫到你! – pbush25