简体   繁体   中英

Data is retrieving from parse database

So I am trying to make an app where people can see which group they are in. So for every group they have made or been added to, it will be getting saved in a class on parse called "grupper". Their username are saved under column "username" and what group they are in as "gruppe".

I have been searching a while for a script which could retrieve their groups in a table view. I am not getting any errors but the table view is just blank.

I cannot point out why it does not retrieve any of the names they are in.

I am very new to stackoverflow and swift so please let me know if i need to provide more information.

Here are my code:

@IBOutlet weak var grupper: UITableView!

var userArray: [String] = []

    func retrieveMessages() {
        let userArray: [String] = []
        let query:PFQuery = PFQuery(className: "grupper")
        let currentUser = query.whereKey("username", equalTo: PFUser.currentUser()!.username!)
        currentUser.findObjectsInBackgroundWithBlock {
            (objects, error) -> Void in

            for object in objects! {
                let username:String? = (object as PFObject)["gruppe"] as? String
                if username != nil {
                    self.userArray.append(username!)
                }
            }
            self.grupper.reloadData()
        }
    }

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

    // MARK: - Table view data source

     func numberOfSectionsInTableView(gruppe: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }

     func grupper(grupper: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return userArray.count
    }


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

        // Update - replace as with as!

        let cell = grupper.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = userArray[indexPath.row]

        return cell
    }

It is not clear when and from which method your retrieveMessages method is being called.

First , make sure that retrieveMessages is called from viewDidLoad (which is called the first time the view is accessed) or from viewWillAppear (which is called every time the view is access).

For example:

class YourController: UIViewController {

    override func viewWillAppear(animated: Bool) {
        retrieveMessages()
    }

    // ... your other code is omitted for brevity ...

}

Second , if you are already doing the above (or something similar) then make sure that the query in your retrieveMessages method is returning data.

You could add a temporary print line to check the size of your userArray array. (Alternatively, you could use the debugger to check.)

For example:

func retrieveMessages() {
   // ... your code is omitted for brevity ...
   print("userArray size = \(userArray.count)")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM