iosmadness
iosmadness

Reputation: 41

How do I manipulate the parse data accordingly?

So basically I am trying to fetch data from parse and show the data accordingly.

How do I view the information and also add information to the tableview of my items.

I have a reservedList class in parse which gives out the userid and username of the person who confirmed a list.

I am not sure how to display the items on the view controller. Everytime i try to print the usernames to the label i get a blank label. and i would like to know how to implement the UITableView to show my data –

gettting error

"Could not cast value of type '__NSArrayM' (0x103cbb8d8) to 'NSString' (0x10412fb20)." when i print out the items.

I have a reservedCust class in parse which contains the following

this it the format I would like to view the data in

var usernames = [String]()
var userids = [String]()
var times = [String]()
var items = [String]()

@IBOutlet var reserveTime: UILabel!
@IBOutlet var reserveUser: UILabel!
@IBOutlet var reservedItems: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()

    let reserveList = PFQuery(className: "reservedList")
    reserveList.findObjectsInBackgroundWithBlock { (objects, error) -> Void in


        if let objects = objects {

            self.usernames.removeAll(keepCapacity: true)
            self.times.removeAll(keepCapacity: true)
            for object in objects {

                if let username = object["username"] as? String {

                    self.usernames.append((object["username"] as? String)!)

                }

                var reservedUser = object["userid"] as? String

                var query = PFQuery(className: "reservedCust")

                query.whereKey("userid", equalTo: reservedUser!)

                query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

                    if let objects = objects {

                        for object in objects {



                            if let time = object["time"] as? String {

                                self.times.append((object["time"] as? String)!)
                            }
                             if let item = object["items"] {
                                self.items.append(object["items"] as! (String))
                            }






                        }
                        print(self.usernames)
                        print(self.times)
                        print(self.items)
                    }




                })
            }
        }



    }


    // Do any additional setup after loading the view.




}

Upvotes: 1

Views: 71

Answers (1)

Saqib Omer
Saqib Omer

Reputation: 5477

First declare two class variables.

var countOfObjects : Int! // Holds count for query objects from Parse

var allObjects : NSArray! // All queried objects from Parse

Than in your viewDidLoad()

override func viewDidLoad() {
    super.viewDidLoad()

    getDataFromParse() // A helper method to fetch data from Parse
    // Initialize for countOfObjects to 0 on viewDidLoad
    countOfObjects = 0
}

func getDataFromParse() {
    let query = PFQuery(className:"YourClassName") 

    query.findObjectsInBackgroundWithBlock { (object : [PFObject]?, error : NSError?) -> Void in

        print(object!.count)

        //set  count Of Objects
        self.countOfObjects = object?.count

        // Set allObjects
        self.allObjects = object

        //Reload TableView after query
        self.YouRTableView.reloadData()
    }

}

// MARK: - TableView DataSource

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

    return countOfObjects
}

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

    let cell = tableView.dequeueReusableCellWithIdentifier(YourCellIdentifier, forIndexPath: indexPath) as! YourTableViewCell

    if let object = allObjects {
        let currentObject = object.objectAtIndex(indexPath.row)
        cell.textLabel?.text = currentObject.valueForKey("somekey") // Assign label text from parse.
      }

    return cell

}

Upvotes: 1

Related Questions