[英]How to display an Image in a tableview using Swift and parse.com
我正在尝试从Parse查询图像到单元格,然后在单击该单元格时查询视图控制器。 在最近使用Swift进行更新之前,我确实已经完成了这项工作,现在我不确定出什么问题了。
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的视图控制器的代码。 图像没有显示,但其他所有内容都出现了。 产品名称显示在正确的位置,当单击单元格时,将带您到视图控制器,其中显示了产品说明,产品名称等。 唯一不显示的是图像。 任何想法可能是什么原因造成的?
尝试使用这个:
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)
}
}
找出我需要做的事
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
}
如果您使用Parse作为后端,则这是Xcode 7.1的更新后的快速代码,可用于将图像拖入表视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.