2015-09-06 32 views
1

我正在構建和應用程序,它使用解析將對象保存在本地數據存儲中。然後,我運行一個查詢來檢索本地數據存儲中的對象,並且它工作正常。但是,我想抓取對象及其中的內容,並根據存儲在解析本地數據存儲對象中的項目在表視圖單元中設置一些標籤。例如,我使用「objectID」,「name」,「date」,「location」等屬性創建對象。我想要做的是在主屏幕上顯示一個表格視圖,顯示名稱,日期,位置等。每個單元中標籤中保存在本地數據存儲中的每個項目的數量。Swift解析 - 本地數據存儲和在桌面視圖中顯示對象

我知道,即時通訊正確保存它:

// parse location object 

    let parseLighthouse = PFObject(className: "ParseLighthouse") 
    parseLighthouse.setObject(PFUser.currentUser()!, forKey: "User") 
      parseLighthouse["Name"] = self.placeTitle.text 
      parseLighthouse["Note"] = self.placeNote.text 
      parseLighthouse["Locality"] = self.placeDisplay.text! 
      parseLighthouse["Latt"] = self.map.region.center.latitude 
      parseLighthouse["Longi"] = self.map.region.center.longitude 
      parseLighthouse["LattDelta"] = 0.5 
      parseLighthouse["LongiDelta"] = 0.5 
      parseLighthouse["Date"] = dateInFormat 
      parseLighthouse.pinInBackground() 
      parseLighthouse.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in 
       println("Object has been saved. ID = \(parseLighthouse.objectId)") 
      } 

,當我運行查詢,即時通訊能夠通過運行的println(object.objectForKey( 「名稱」))來訪問屬性

func performQuery() { 
    let query = PFQuery(className: "ParseLighthouse") 

    query.fromLocalDatastore() 
    query.whereKey("User", equalTo: PFUser.currentUser()!) 
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in 
     if error == nil { 
      // The find succeeded. 
      println("Successfully retrieved \(objects!.count) lighthouses.") 
      // Do something with the found objects 
      if let light = objects as? [PFObject] { 
       for object in light { 
        println(object.objectId) 
        println(object.objectForKey("Name")) 



       } 
      } 
     } else { 
      // Log details of the failure 
      println("Error: \(error!) \(error!.userInfo!)") 
     } 
    } 

,因爲當運行查詢時,我按照預期返回對象ID和名稱。

成功檢索到2座燈塔。 可選(「A3OROVAMIj」) 可選(高興) 可選(「bbyqPZDg8W」) 可選(日期測試)

什麼,我想這樣做是解析對象的本地數據存儲中搶名稱字段,以及是表視圖控制器中單元格上標籤的名稱。

我不知道如何訪問該對象的信息,並正確設置標籤。

沒有人知道這是可能的嗎?

+0

你有你的tableView準備好了嗎?那麼你可以開始訪問解析數據並在其中顯示數據?或者你問如何使用parse.com數據做tableView? –

+0

你是說你得到一個叫做「object」的對象,但它實際上是一個你正在使用的自定義類對象?你有沒有嘗試將它投射到實際的課程並以這種方式訪問​​屬性?在Objective C中,你可以像這樣訪問name屬性:((PFUser *)object).name,儘管我不確定swift是什麼。 –

+0

所以你查詢功能是否正常工作,你應該想要獲得這些信息,然後加載你的tableview中的所有單元格......對嗎? – Lamar

回答

1

它總是一個好主意,以避免指針笑...那麼,爲什麼不保存用戶ID或用戶名與特定對象.. 所以改變這一行:

parseLighthouse.setObject(PFUser.currentUser()!, forKey: "User") 

TO

parseLighthouse["username"] = PFUser.currentUser().username 

回答

現在我們來創建一個包含對象ID和th的結構e名稱以外的控制器類。

struct Data 
{ 
var Name:String! 
var id:String! 
} 

然後Controller類的內部,聲明下面的代碼行全球

var ArrayToPopulateCells = [Data]() 

然後將查詢功能將類似於:

func performQuery() { 
    let query = PFQuery(className: "ParseLighthouse") 

    query.fromLocalDatastore() 
    query.whereKey("User", equalTo: PFUser.currentUser()!) 
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in 
     if error == nil { 
      // The find succeeded. 
      print("Successfully retrieved \(objects!.count) lighthouses.") 
      // Do something with the found objects 
      if let light = objects as? [PFObject] { 
       for object in light { 
        print(object.objectId) 
        print(object.objectForKey("Name")) 
        var singleData = Data() 
        singleData.id = object.objectId 
        singleData.Name = object["Name"] as! String 

        self.ArrayToPopulateCells.append(singleData) 


       } 
      } 
     } else { 
      // Log details of the failure 
      print("Error: \(error!) \(error!.userInfo)") 
     } 
    } 

TableView中numberOfRowinSection()

return ArrayToPopulateCells.count 

在的cellForRowAtIndexPath()

 var data = ArrayToPopulateCells[indexPath.row] 
     cell.textlabel.text = data.objectID 
     cell.detailLabel.text = data.Name 

瞧這應該是它

+0

好吧,我按照您的指示行事,他們似乎沒有問題,沒有錯誤。但在表格視圖中沒有任何顯示。我完全遵循了這些代碼,並且沒有任何內容。即時通訊不完全確定項目被添加到arrayToPopulateCells數組。有任何想法嗎? – Bcavanaugh934

+0

你能更新你的代碼嗎 – Lamar

+0

你是否重新加載了桌面視圖 – Lamar

相關問題