2016-01-08 100 views
0

我使用解析的PFQueryTableViewController,似乎都工作正常,當檢索日期,除了UIImageView的沒有加載數據庫中的文件。文本和其他所有內容都會加載,但文件除外。這裏是我的代碼:解析PFQueryTableViewConntroller不加載圖像

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" { 



     } 

    } 

} 

enter image description here

回答

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

我嘗試這個代碼,並沒有什麼似乎有已更改,並且控制檯中未打印任何錯誤。 –