2017-01-27 33 views
0

我做了一個表視圖控制器的查詢對象的雲,代碼順利,但問題是,當視圖加載分析查詢不顯示在表上。表分析查詢不出現

我嘗試了一些解決方案,但大多數解決方案都是針對USER類的,不適用於任何其他自定義類。

@IBOutlet weak var cloudsTable: UITableView! 

var clouds: [PFObject] = [PFObject]() 

func loadClouds() { 

    let cloudsQuery = PFQuery(className: "_Clouds") 
    cloudsQuery.order(byAscending: "createdAt") 

    cloudsQuery.findObjectsInBackground(block: { (result, error) in 


     if error == nil 
     { 
      self.clouds = result! 
      self.cloudsTable.reloadData() 
      print(self.clouds) 
     } 
    }) 
} 

此功能是調用雲類檢索值查看

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     return clouds.count 

    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cloudsCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 

     let userObject:PFObject = clouds[indexPath.row] 

     cloudsCell.textLabel?.text = userObject.object(forKey: "user") as? String 

     return cloudsCell 

    } 

這傳遞解析查詢表函數。

+0

有:'cloudsTable.delegate'&'cloudsTable.datasource'設置?你應該在主線程中調用'reloadData()'。 – Larme

+0

@Larme不,我沒有設置它們,你的意思是我必須在子類上設置cloudsTable.delegate和cloudsTable.datasource?或在視圖上加載功能? – user2508528

+0

如果你使用Storyboard,你可以在那裏做。 – Larme

回答

0

請執行以下操作。我已經稍微修改了你的代碼。

確保你有你的delegatedatasource集。在你的情況下,你的雲出路。

enter image description here

@IBOutlet weak var cloudsTable: UITableView! 

var clouds = [PFObject]() 

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

func loadClouds() { 
     clouds.removeAll() 
     let cloudsQuery = PFQuery(className: "Clouds") //Make sure class name is correct 
     cloudsQuery.order(byAscending: "createdAt") 
     cloudsQuery.findObjectsInBackground(block: { (result, error) in 
      if error == nil 
      { 
       self.clouds = result! 
       self.cloudsTable.reloadData() 
       print(self.clouds) 
      } 
     }) 
} 
func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
} 


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return clouds.count 

} 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cloudsCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 

      var recClass = PFObject(className: "Clouds") 
       recClass = clouds[(indexPath as NSIndexPath).row] 

       cloudsCell.textLabel?.text = recClass["user"] as? String 

       return cloudsCell 
} 
+0

我仍然有相同的錯誤,在這裏錯誤消息:無效的類名:_Clouds,類名只能包含字母數字字符和_,並且必須以字母字符開頭(代碼:103,版本:1.14.2) 錯誤域=解析代碼= 103「無效的類名:_Clouds,類名只能包含字母數字字符和_,並且必須以字母開頭」UserInfo = {code = 103,temporary = 0,error =無效的類名:_Clouds,類名只能包含字母數字字符和_,並且必須以字母字符開頭 – user2508528

+0

這是因爲類名錯誤!我在代碼中對其進行了評論你去檢查一下。 – Cliffordwh

+0

應該是雲。但去檢查scheama以確保 – Cliffordwh

0

我會用PFQueryTableViewController分析查詢數據加載到表中。 Parse理解查詢和將數據加載到表中是一種流行的功能,他們創建了一個非常簡單的類來處理這個問題。查看下面的指南並尋找PFQueryTableViewController。它處理從數據加載/重新加載/緩存的一切,所以值得一看。如果您對如何實施它有任何疑問,請告訴我。

https://parseplatform.github.io/docs/ios/guide/