Archie
Archie

Reputation: 95

All Parse Objects Not Presented in UITableView

I am retrieving objects from a relation in parse. The objects I want are successfully retrieved and printed in the output box, but when I run the app my UITable only presents one of the six objects. Any suggestions on how to get all of them up onto my view? I would greatly appreciate it.

 class MyGroupsHomePage: UITableViewController {

let cellidentifer = "MyGroupsCell"


var mygroupsdata: NSMutableArray = NSMutableArray()

func findcurrentuserobjects () {
    var currentuser = PFUser.query()
    currentuser!.whereKey("username", equalTo: PFUser.currentUser()!.username!)
    currentuser!.findObjectsInBackgroundWithBlock { (object:[AnyObject]?, error: NSError?) -> Void in
        if error == nil && object != nil {

            if let object = object as? [PFObject] {

                for objects in object {

                   self.mygroupsdata.addObject(objects)
                }

            }
        }

        self.tableView.reloadData()

    }


}



override func viewDidLoad() {
    super.viewDidLoad()

    findcurrentuserobjects()



}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.mygroupsdata.count
}

var groupnamearray: NSMutableArray = NSMutableArray()
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellidentifer, forIndexPath: indexPath) as! UITableViewCell
    let mygroupdata: PFObject = self.mygroupsdata.objectAtIndex(indexPath.row) as! PFObject
    let relation = mygroupdata.relationForKey("UserGroups")
    let query = relation.query()
    query?.findObjectsInBackgroundWithBlock({ (objet:[AnyObject]?, erro: NSError?) -> Void in

        if erro == nil && objet != nil {

            if let objet = objet as? [PFObject] {

                for objets in objet {
                    println(objets.objectForKey("GroupName")!)
                    cell.textLabel?.text = objets.objectForKey("GroupName")! as? String

                }

            }

        } else {

            println("Error, could not retrieve user groups \(erro)")

        }



    })


    return cell
}








 }

Upvotes: 1

Views: 63

Answers (1)

Eppilo
Eppilo

Reputation: 773

As Paulw11 stated, this is the problem:

for objets in objet {
   println(objets.objectForKey("GroupName")!)
   cell.textLabel?.text = objets.objectForKey("GroupName")! as? String
}

You keep updating the same property "text" in the same textLabel, which I assume is an IBOutlet in the UITableViewCell subclass that you use to define the apparence of your cell. Without knowing more of how you want this text to be layed out it it difficult to suggest an answer. A quick and dirty way could be (I haven't tested):

for objets in objet {
   println(objets.objectForKey("GroupName")!)
   let obj = objets.objectForKey("GroupName")! as? String
   let newString = "\(obj) "
   cell.textLabel?.text = "\(cell.textLabel?.text)\(newString)"
}

But, according to what you want to acheive, you might need to add subviews to your UITableViewCell subclass (either on your cell prototype in Storyboard or programmatically).

Upvotes: 1

Related Questions