Swift Parse - local datastore and displaying objects in a tableview

与世无争的帅哥 提交于 2019-12-02 03:00:48

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")

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.

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

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)")
        }
    }

In the tableView numberOfRowinSection()

return ArrayToPopulateCells.count

In the cellForRowAtIndexPath()

       var data = ArrayToPopulateCells[indexPath.row]
       cell.textlabel.text = data.objectID
       cell.detailLabel.text = data.Name

VOila that should be it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!