Swift在TableView单元格中显示解析图像

时间:2015-04-26 21:09:36

标签: ios swift parse-platform

我正在尝试显示保存到解析属性“image”的用户图像。我已经能够显示我的用户名没有问题,但我似乎无法让我的图像出现。我应该将这些信息作为UIImage投射吗?我正确地调用文件的存储位置吗?

这是我的代码:

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

2 个答案:

答案 0 :(得分:5)

照片保存在PFFile中而不是UIImage ..

是什么让您的代码如下:

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
    }

查看更多in the docs

答案 1 :(得分:-1)

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