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

标签 ios swift parse-platform

我正在尝试显示为解析属性“图像”而保存的用户图像。我已经能够毫无问题地显示我的用户名,但我似乎无法让我的图像出现。我应该将此信息转换为 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)            
    }       
}

最佳答案

照片保存在 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

关于ios - Swift 在 TableView 单元格中显示解析图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29883567/

相关文章:

swift - 关闭数字键盘

swift - 我可以强制 Swift 代码使用所有内核吗?

android - 如何在 Parse.com 中验证 deviceToken 的 GCM 发件人 ID

ios - 从 Beacon Enter 上的暂停状态唤醒应用程序花费的时间太长

ios - (null) 和 <null> 之间的区别

ios - 如何让 Firebase 自动删除超过 30 分钟的值

google-app-engine - 在 GAE 中使用 Parse REST API 时出现 SocketTimeoutException

ios - Cordova iOS - 即使创建了文件也无法录制媒体

ios - 如何自定义collectionView单元格?

javascript - 是否可以从云中调用 Parse 定义的云代码函数?