user5556622
user5556622

Reputation:

Data retrieval using Parse in Swift 2

I did this for fetching data and showing it in a table view but it's not showing anything.

I used this code:

import UIKit
import Parse
import Bolts

class Parsedata: UIViewController, UITableViewDelegate, UITableViewDataSource {
    //@IBOutlet var NTableView: UITableView!

    @IBOutlet var NTableView: UITableView!
    var NArray:[String] = [String]()




    override func viewDidLoad() {
        super.viewDidLoad()



        // Do any additional setup after loading the view, typically from a nib.

        self.NTableView.delegate = self
        self.NTableView.dataSource = self


        // retrieve notification from parse
        self.RetrieveN()
        NSLog("Done with it")

    }

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

    func RetrieveN () {

        //create a pfquery
        var query:PFQuery = PFQuery(className: "Notification")

        //call findobject in background
        query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

            //clear the Narray

            self.NArray = [String]()

            //loop through the objects array
            for Nobject in objects!{

                //retrieve the text column value of each PFobject
                let Ntext:String? = (Nobject as! PFObject) ["Text"] as? String


                // assign it into your Narray

                if Ntext != nil {
                self.NArray.append(Ntext!)

                }

            }

            if error == nil {
                // The find succeeded.
                print("Successfully retrieved \(objects!.count) Notifications.")}
           //reload the table view
            self.NTableView.reloadData()
            }
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = self.NTableView.dequeueReusableCellWithIdentifier("NCell") as UITableViewCell?

        cell?.textLabel?.text = self.NArray[indexPath.row]

        return cell!

    }


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

        return NArray.count
    }
}

It's working fine because it's showing that 3 objects were retrieved on the LOG container.

Upvotes: 0

Views: 180

Answers (2)

Eli
Eli

Reputation: 768

I have just learnt how to do this myself, this is how you can load data from parse and save it as a NSMutableArray then use that data to populate your table view.

let NArray : NSMutableArray = NSMutableArray() 

then you can use your query you made with

var query:PFQuery = PFQuery(className: "Notification")

query.findObjectsInBackgroundWithBlock( { (AnyObject objects, NSError error) in
        if error == nil {
            self.NArray = NSMutableArray(array: objects!)
            self.NTableView.reloadData()
        } else {
          print(error?.localisedDescription)
    })

this will load all your content into the variable NArray, which you can then use in your tableView function with indexPath. That line of code inside your tableView cellForRowAtIndexPath would be

cell?.textLabel?.text = self.NArray[indexPath.row] //you may have to cast this as? String

Like i said this is how i am loading my data, i am using this to load PFUser usernames, profile pictures etc and displaying them in a custom table view cell. You may have to slightly tweak these methods but the base methods are there

Upvotes: 0

pbush25
pbush25

Reputation: 5248

Unless you have more code than what's posted here, you also need to implement numberOfSectionsInTableView and return 1

Upvotes: 1

Related Questions