简体   繁体   中英

Retrieving User Profile Image and Username in TableView from Parse - Swift

I am able to fetch users and their profile pictures however, I'd like to return just the current user in the table and it doesn't seem possible since I'm using NSMutableArray to hold the user value.

UPDATE: I've edited to show my complete class

var userArray : NSMutableArray = []

 override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self


    loadParseData()

    if self.revealViewController() != nil {
        menuButton.target = self.revealViewController()
        menuButton.action = "revealToggle:"
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    }
    // Do any additional setup after loading the view.
}

    func loadParseData() {

    var query : PFQuery = PFUser.query()!

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

        if error == nil {
            if let objects = objects {

                for object in objects {
                    self.userArray.addObject(object)
                }
                self.tableView.reloadData()
            }
        } else {
            println("There is an error")
        }
    }
}


// Initialise the PFQueryTable tableview
override init(style: UITableViewStyle, className: String!) {
    super.init(style: style, className: className)
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)


    //Configure PFQueryTableView
    self.parseClassName = "User"
    self.pullToRefreshEnabled = true
    self.paginationEnabled = false

}

// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery {

    var query = PFQuery(className: "User")
    return query
}


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

    return 1

}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //How can I return PFUser.currentUser() here?   
    return self.userArray.count


}

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

    let row = indexPath.row

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


    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ProfileTableViewCell


  //Display Profile Image
    PFUser.currentUser()?["profile_picture"]
    if let pfimage = PFUser.currentUser()?["profile_picture"] as? PFFile{
        pfimage.getDataInBackgroundWithBlock({
            (result, error) in
            cell.profileImage.image = UIImage(data: result!)
            cell.userName.text = username              

        })
    }

    return cell
}

You may declare your array as an AnyObject array. Try this:

var userArray : AnyObject = []

Try using some breakpoints to take a look on the variables and understand whats going on.

Also, if you have only one section on your Table View there is no need on calling numberOfSectionsInTableView since the defaul is 1 .

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