Swift Parse – 本地数据存储区并在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”)可选(日期测试)

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

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

有谁知道这有可能吗?

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

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

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

回答

现在让我们创建一个包含objectID和 Controller类之外的Name的结构。

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

然后在Controller类内部,全局声明以下代码行

  var ArrayToPopulateCells = [Data]() 

然后您的查询function将如下所示:

  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应该是它