簡體   English   中英

Swift Parse-本地數據存儲並在表視圖中顯示對象

[英]Swift Parse - local datastore and displaying objects in a tableview

我正在構建和應用程序,用於通過解析將對象保存在本地數據存儲中。 然后,我運行查詢以檢索本地數據存儲中的對象,並且工作正常。 但是,我想抓取該對象及其中的內容,並根據存儲在解析本地數據存儲對象中的項目在表視圖單元格中設置一些標簽。 例如,我制作一個具有“ objectID”,“ name”,“ date”,“ location”等屬性的對象。 我想做的是在主屏幕上顯示一個表格視圖,其中顯示名稱,日期,位置等。 每個單元格標簽中保存在本地數據存儲區中的每個項目的數量。

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

// 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(“ Name”))訪問屬性

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”)可選(日期測試)

我想做的是獲取解析對象本地數據存儲中的名稱字段,並將其作為表視圖控制器中單元格上的標簽名稱。

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

有誰知道這怎么可能?

始終避免使用指針大聲笑是一個好主意...因此,為什么不將userid或username與特定對象一起保存..因此更改此行:

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

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

回答

現在,讓我們創建一個結構,該結構包含ControllerID外部的objectID和Name

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

瞧,應該是這樣

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM