2015-04-26 43 views
1

我正在嘗試顯示保存到解析屬性「image」的用戶圖像。我已經能夠顯示我的用戶名沒有問題,但我似乎無法讓我的圖像出現。我應該把這些信息作爲UIImage嗎?我是否正確調用了文件的存儲位置?Swift在TableView Cell中顯示解析圖像

這裏是我的代碼:

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

我可能錯了,但我認爲它下載爲PFFile? – Paulo

+0

在你的解析數據庫中,圖像列有什麼類型?它可能是一個PFFile,但也可以被引用爲一個URL(可能更好),然後您可以從中下載圖像。 – BHendricks

+0

是的,我相信PFFile,當我查看數據庫時,它有數據類型的字節數。 – cphill

回答

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

+0

嘿@RaVeN謝謝你的迴應。這對我來說比我的其他方法更有意義,但是當我作爲「PFFile」進行投票時,該應用會崩潰。這是我的錯誤信息:'不能將'_NSInlineData'(0x107dd1cf8)類型的值轉換爲'PFFile'(0x106216e80)。'有關爲什麼會發生這種情況的任何想法? – cphill

+0

@cphill您的分析數據庫中的列是「文件」列嗎? 如果沒有,請更改 – siegy22

+0

是的,@RaVeN,這是我遇到的問題。非常感謝您花時間回覆我的問題 – cphill

-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() 
      } 
     } 
    } 
} 
相關問題