[英]Swift Parse - local datastore and displaying objects in a tableview
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". 例如,我制作一个具有“ 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)")
}
and when i run the query, im able to access the attributes by running println(object.objectForKey("Name")) 当我运行查询时,无法通过运行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!)")
}
}
because when running the query, i get back the object id and name as expected. 因为在运行查询时,我按预期获得了对象ID和名称。
Successfully retrieved 2 lighthouses. 成功检索了2座灯塔。 Optional("A3OROVAMIj") Optional(happy) Optional("bbyqPZDg8W") Optional(date test) 可选(“ A3OROVAMIj”)可选(快乐)可选(“ bbyqPZDg8W”)可选(日期测试)
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? 有谁知道这怎么可能?
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: 始终避免使用指针大声笑是一个好主意...因此,为什么不将userid或username与特定对象一起保存..因此更改此行:
parseLighthouse.setObject(PFUser.currentUser()!, forKey: "User")
TO 至
parseLighthouse["username"] = PFUser.currentUser().username
Answer 回答
NOW let's create a struct that contains the objectID and the Name outside of your Controller Class. 现在,让我们创建一个结构,该结构包含ControllerID外部的objectID和Name 。
struct Data
{
var Name:String!
var id:String!
}
then inside of the Controller class, declare the following line of code globally 然后在Controller类内部,全局声明以下代码行
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)")
}
}
In the tableView numberOfRowinSection() 在tableView numberOfRowinSection()中
return ArrayToPopulateCells.count
In the cellForRowAtIndexPath() 在cellForRowAtIndexPath()中
var data = ArrayToPopulateCells[indexPath.row]
cell.textlabel.text = data.objectID
cell.detailLabel.text = data.Name
VOila that should be it 瞧,应该是这样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.