我正在使用parse的PFQueryTableViewController,在检索日期时似乎都工作正常,除了UIImageView没有加载数据库中的文件。除文件外,文本和其他所有内容都会加载。这是我的代码:
import UIKit
import Parse
import ParseUI
class PostsTableViewController: PFQueryTableViewController {
override func queryForTable() -> PFQuery {
let query = PFQuery(className: "Posts")
query.cachePolicy = .CacheElseNetwork
query.orderByDescending("createdAt")
return query
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! PostsTableViewCell
let posterUsernameText = object?.objectForKey("createdBy") as? String
cell.posterUsername.setTitle("\(posterUsernameText)", forState: .Normal)
cell.msgTextView.text = object?.objectForKey("text") as? String
let imageFile = object?.objectForKey("image") as? PFFile
cell.cellImageView.file = imageFile
cell.imageView?.loadInBackground()
return cell
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row + 1 > self.objects?.count {
return 44
}
let height = super.tableView(tableView, heightForRowAtIndexPath: indexPath)
return height
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row + 1 > self.objects?.count {
self.loadNextPage()
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else {
self.performSegueWithIdentifier("showDetail", sender: self)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
}
}
}
答案 0 :(得分:0)
我认为您应该更改此代码:
let imageFile = object?.objectForKey("image") as? PFFile
cell.cellImageView.file = imageFile
cell.imageView?.loadInBackground()
要:
if let imageFile = object?.objectForKey("image") as? PFFile {
imageFile.getDataInBackgroundWithBlock({ (imgContent:NSData?, error:NSError?) -> Void in
if error != nil {
print(error?.description)
}else if let imgContent = imgContent {
cell.imageView?.image = UIImage(data: imgContent)!
}
})
}
答案 1 :(得分:0)
稍微讨论代码后,这似乎有效。希望这对未来的其他人有所帮助,这里是我所做的改变:
而不是:
let imageFile = object?.objectForKey("image") as? PFFile
cell.cellImageView.file = imageFile
cell.imageView?.loadInBackground()
我应该是:
let imageFile = object?.objectForKey("image") as? PFFile
cell.cellImageView.file = imageFile
cell.cellImageView?.loadInBackground()