2015-10-21 55 views
1

我試圖從Parse查詢圖像到我的單元格,然後單擊該單元格時查看控制器。在最近使用Swift進行更新之前,我確實有這個工作,現在我不確定是哪裏出了問題。如何使用Swift和parse.com在tableview中顯示圖像

import UIKit 
import Parse 
import ParseUI 
import Bolts 

class ProductTableViewController: PFQueryTableViewController { 

    // 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 the PFQueryTableView 
     self.parseClassName = "Products" 
     self.textKey = "productName" 
     self.imageKey = "productImage" 
     self.pullToRefreshEnabled = true 
     self.paginationEnabled = false 

    } 



    // Define the query that will provide the data for the table view 
    override func queryForTable() -> PFQuery { 
     let query = PFQuery(className: "Products") 
     query.orderByDescending("createdAt") 
     return query 
    } 


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell { 

     var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! Customcells! 
     if cell == nil { 
      cell = Customcells(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell") 
     } 

     // Extract values from the PFObject to display in the table cell 
     if let ProductName = object?["ProductName"] as? String { 
      cell.ProductName.text = ProductName 
     } 


     // Display product image 
     let initialThumbnail = UIImage(named: "question2") 
     cell.productImage.image? = initialThumbnail! 
     if let thumbnail = object?["ProductImage"] as? PFFile { 
      cell.productImage.file = thumbnail 
      cell.productImage.loadInBackground() 
     } 



     return cell 
    } 



    override func viewDidLoad() { 
     super.viewDidLoad() 



    } 

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



    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

     // Get the new view controller using [segue destinationViewController]. 
     let productView = segue.destinationViewController as! ProductViewController 

     // Pass the selected object to the destination view controller. 
     if let indexPath = self.tableView.indexPathForSelectedRow { 
      let row = Int(indexPath.row) 
      productView.currentObject = (objects?[row] as! PFObject) 



     } 
    } 

} 

這是我的一個視圖控制器的代碼,它有tableView。圖像沒有出現,但其他一切都出現了。產品名稱顯示在正確的位置,當單元格被點擊時,它會將您帶到視圖控制器,向您顯示產品說明,產品名稱等。唯一沒有出現的是圖像。任何想法可能會造成這種情況?

回答

0

發現了什麼,我需要如果您使用解析爲您的後端,這是更新迅速代碼的Xcode 7.1做

override func tableView(tableView: UITableView, cellForRowAtIndexPath  indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! Customcells 


    let imageFile = object?.objectForKey("ProductImage") as? PFFile 
    cell.productImage.image = UIImage(named: "question2") 
    cell.productImage.file = imageFile 
    cell.productImage.loadInBackground() 


    return cell 

} 

,你可以用於將圖像拖入桌面視圖。

0

嘗試使用此:

let profileImage:PFFile = Places[indexPath.row]["Image"] as! PFFile 
profileImage.getDataInBackgroundWithBlock { 
      (imageData: NSData?, error: NSError?)-> Void in 
      if (error == nil) { 
       cell.pic.setImage(UIImage(data: imageData!)!, forState: UIControlState.Normal) 
      } 
     } 
+0

感謝您的幫助,但您可以幫我弄清楚我的代碼會在哪裏出現在這裏?對不起,我只是相當新的解析和迅速 – RedLineTom

相關問題