简体   繁体   中英

Swift Displaying Parse Image in TableView Cell

I am attempting to display the users image that is saved to parse property "image". I have been able to display my username with no issue, but I can't seem to be able to get my image to appear. Should I be casting this information as UIImage? Am I correctly calling where the file is stored?

Here is my code:

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()

        var user = PFUser.currentUser()
    }

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


    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            
        var individualUser = userArray[row] as! PFUser
        var username = individualUser.username as String

        var profilePicture = individualUser["image"] as? UIImage            
        cell.userImage.image = profilePicture            
        cell.usernameLabel.text = username

        return cell
    }

    @IBAction func finishAddingUsers(sender: AnyObject) {
        self.performSegueWithIdentifier("finishAddingUsers", sender: self)            
    }       
}

The photos are saved in a PFFile and not as a UIImage..

What makes your code the following:

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

        let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! SearchUsersRegistrationTableViewCell

        let row = indexPath.row            
        var individualUser = userArray[row] as! PFUser
        var username = individualUser.username as String

        var pfimage = individualUser["image"] as! PFFile

        pfimage.getDataInBackgroundWithBlock({
            (result, error) in
            cell.userImage.image = UIImage(data: result)
        })        
        cell.usernameLabel.text = username

        return cell
    }

See more in the docs

 fileprivate func getImage(withCell cell: UITableViewCell, withURL url: String) { Alamofire.request(url).responseImage { (image) in /* Assign parsed Image */ if let parsedImage = image.data { DispatchQueue.main.async { /* Assign Image */ cell.imageView?.image = UIImage(data: parsedImage) /* Update Cell Content */ cell.setNeedsLayout() cell.layoutIfNeeded() } } } } 

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