Swift Parse - 本地数据存储区并在tableview中显示对象

时间:2015-09-06 22:50:00

标签: ios swift parse-platform local-datastore

我正在构建和应用程序,使用解析将对象保存在本地数据存储区中。然后我运行一个查询来检索本地数据存储区中的对象,它工作正常。但是,我想抓取对象及其中的内容,并根据存储在解析本地数据存储对象中的项目在表视图单元格中设置一些标签。例如,我创建一个具有" 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&#34) 可选(高兴) 可选(" bbyqPZDg8W&#34) 可选(日期测试)

我想要的是获取parse对象本地数据存储中的name字段,这是表视图控制器中单元格上的标签名称。

我不知道如何从对象访问该信息,并正确设置标签。

有谁知道这是怎么回事?

1 个答案:

答案 0 :(得分:1)

避免指针lol总是一个好主意...所以为什么不用特定对象保存用户标识或用户名。 所以改变这一行:

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

TO

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

<强>答案

现在让我们创建一个结构,其中包含控制器类之外的 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

应该是VOila