0

Having a bit of trouble trying to push objects that I query from Parse into an array that I can use in a UITableView.

Here's my code.

var locations = [AnyObject]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Query the Locations class.
    let query = PFQuery(className:"Location")

    query.findObjectsInBackground {
        (objects: [PFObject]?, error: Error?) -> Void in
        if error == nil {
            if let objects = objects {
                for object in objects {
                    self.locations.append(object)
                }
                self.venueTable.reloadData()
            }
        } else {
            // Log details of the failure
            print("Error: (error!) (error!.userInfo)")
        }
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return locations.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let locationCell = tableView.dequeueReusableCell(withIdentifier: "locationCell", for: indexPath)

    let location = locations[indexPath.row]

    locationCell.textLabel?.text = location

    return locationCell
}

After the for loop, locations is full of the parse data, but not sure how to access it when pushing it to the locationCell

0

1 Answer 1

3

The type you set for locations is [AnyObject] so that won't work when you try to set the labels text property as it isn't a string.

Instead set it to [PFObject] and then use PFObject's function objectForKey to get the relevant string value from your retrieved object.

Eg

var locations = [PFObject]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Query the Locations class.
    let query = PFQuery(className:"Location")

    query.findObjectsInBackground {
    (objects: [PFObject]?, error: Error?) -> Void in
        if error == nil {
            if let objects = objects {

                self.locations = objects

                self.venueTable.reloadData()

            }

         } else {
        // Log details of the failure
        print("Error: (error!) (error!.userInfo)")
        }

    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

     return locations.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let locationCell = tableView.dequeueReusableCell(withIdentifier: "locationCell", for: indexPath)

    let location = locations[indexPath.row]

    locationCell.textLabel?.text = location.objectForKey("property name here") as? String

    return locationCell
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.