cphill
cphill

Reputation: 5914

Swift parse query results not appearing in tableview

I am running into difficulty displaying the data from the query I made in the individual cells of my tableview. I believe that my logic is correct, but I'm not seeing the console.log's that I am calling within my function that contains the Parse queried data. This might be a simple fix, but it isn't coming to me at the moment. The console log I should be seeing to validate that my query is coming through correctly is the println("\(objects.count) users are listed"), it should then be displayed within the usernameLabel.text property.

import UIKit

class SearchUsersRegistrationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var userArray : NSMutableArray = []

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
        loadParseData()

    }

    func loadParseData() {
        var query : PFQuery = PFUser.query()

        query.findObjectsInBackgroundWithBlock {
            (objects:[AnyObject]!, error:NSError!) -> Void in

            if error == nil {
                if let objects = objects { 
                    println("\(objects.count) users are listed")
                    for object in objects {
                        self.userArray.addObject(object)
                    }
                    self.tableView.reloadData()
                }
            } else {
                println("There is an error")
            }
        }
    }

    let textCellIdentifier = "Cell"

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

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! SearchUsersRegistrationTableViewCell
        let row = indexPath.row
        let cellDataParse : PFObject = self.userArray.objectAtIndex(row) as! PFObject

        //cell.userImage.image = UIImage(named: usersArr[row])
        cell.usernameLabel.text = cellDataParse.objectForKey("_User") as! String

        return cell
    }
}

Upvotes: 1

Views: 627

Answers (1)

cphill
cphill

Reputation: 5914

I fixed the issue. I needed to cast the index path row in the users array as a PFUser and then cast the user's username property as a String and then set that as the label text.

        let row = indexPath.row

        var user = userArray[row] as! PFUser
        var username = user.username as String

        cell.usernameLabel.text = username

Upvotes: 1

Related Questions