Swift Parse - 本地数据存储并在 tableview 中显示对象 [英] Swift Parse - local datastore and displaying objects in a tableview
问题描述
我正在构建和应用程序,通过解析将对象保存在本地数据存储中.然后我运行一个查询来检索本地数据存储中的对象,并且它工作正常.但是,我想获取对象及其中的内容,并根据存储在解析本地数据存储对象中的项目在表视图单元格中设置一些标签.例如,我创建了一个具有objectID"、name"、date"、location"等属性的对象.我想要做的是在主屏幕上有一个表格视图,显示名称、日期、位置等.保存在本地数据存储区中每个单元格的标签中的每个项目.
I am building and app that saves an object in the local datastore with parse. I then run a query to retrieve the objects that are in the local datastore and it is working fine. however, I would like to grab the object, and the contents in it, and set some labels in a table view cell based on the items that are stored in the parse local data store object. for example, i make an object with attributes like "objectID", "name", "date", "location". what i'd like to do is to have a table view on the home screen that displays the name, date, location ...etc. of each item that was saved in local datastore in labels in each cell.
我知道我正确地保存了它:
i know that im saving it correctly:
// 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")) 来访问属性
and when i run the query, im able to access the attributes by running 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 和名称.
because when running the query, i get back the object id and name as expected.
成功找回了 2 个灯塔.可选(A3OROVAMIj")可选(快乐)可选(bbyqPZDg8W")可选(日期测试)
Successfully retrieved 2 lighthouses. Optional("A3OROVAMIj") Optional(happy) Optional("bbyqPZDg8W") Optional(date test)
我想要做的是获取解析对象本地数据存储中的名称字段,它是表视图控制器中单元格上的标签名称.
what I would like to do is grab the name field within the parse object local data store, and that be the name of the label on a cell in a table view controller.
我不知道如何从对象访问该信息,并正确设置标签.
i dont know how to access that info from the object, and set the label correctly.
有人知道这是怎么可能的吗?
does anyone know how this is possible?
推荐答案
避免使用指针总是一个好主意,哈哈……那么为什么不使用特定对象保存用户 ID 或用户名呢..所以改变这一行:
It's always a good idea to avoid pointer lol ... so why not saving the userid or username with the specific object.. so change this line:
parseLighthouse.setObject(PFUser.currentUser()!, forKey: "User")
到
parseLighthouse["username"] = PFUser.currentUser().username
回答
现在让我们在控制器类之外创建一个包含 objectID 和 Name 的结构.
NOW let's create a struct that contains the objectID and the Name outside of your Controller Class.
struct Data
{
var Name:String!
var id:String!
}
然后在Controller类里面,全局声明下面这行代码
then inside of the Controller class, declare the following line of code globally
var ArrayToPopulateCells = [Data]()
那么您的查询函数将如下所示:
Then your query function will look like :
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应该是这样
这篇关于Swift Parse - 本地数据存储并在 tableview 中显示对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!