2015-05-20 46 views
0

夥計們。我解析了一個問題,我從Parse得到了一個圖像,但是圖像是錯誤的,有時會出現相同的圖像,其他圖像出現錯誤,我不知道發生了什麼,有人幫助我!我是Parse和Swift的新手。謝謝!解析錯誤的圖像,異步

進口的UIKit 進口解析 進口螺栓 進口螺栓 進口解析

類LojasViewController:UIViewController的,的UITableViewDelegate {

var ParseData = [PFObject]() 
var dataObjects = [NSData]() 
var photoArray: Array<UIImage> = [] 
var ImagemCelula = UIImage() 


@IBOutlet weak var saloesTableView: UITableView! 


override func viewDidLoad() { 
    super.viewDidLoad() 

    GetParse() 


} 


override func viewDidAppear(animated: Bool) { 
    // Refresh the table to ensure any data changes are displayed 
    saloesTableView.reloadData() 
} 




// Retrieve from Parse 
func GetParse() { 

    var query:PFQuery = PFQuery(className:"Saloes") 
    query.orderByAscending("nome") 
    query.findObjectsInBackgroundWithBlock { 
     (objects: [AnyObject]?, error: NSError?) -> Void in 

     if error == nil { 
      if let object = objects as? [PFObject] { 
       self.ParseData = object 

       println(self.ParseData.count) 
       println(self.ParseData) 
       self.saloesTableView.reloadData() 

      } 
     } 
    } 
} 



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


// Esconde statusBar 
override func prefersStatusBarHidden() -> Bool { 
    return true 
} 


@IBAction func voltarButton(sender: AnyObject) { 
    self.dismissViewControllerAnimated(true, completion: nil) 
} 





// MARK: - Table view data source ____________________________________ 

func numberOfSectionsInTableView(tableView: UITableView!) -> Int 
{ 
    return 1 
} 




func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int 
{ 
    return ParseData.count 
} 




func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? 
{ 
    let cell:SaloesTableViewCell = tableView!.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath!) as! SaloesTableViewCell 

    cell.nomeSalao.text = ParseData[indexPath!.row]["nome"] as? String 

    for object in ParseData { 
     let ImageFile = object["imagemCelula"] as! PFFile 
     ImageFile.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in 

      if let imageData = imageData where error == nil 
      { 

       self.ImagemCelula = UIImage(data: imageData)! 
       cell.imagemSalao.image = self.ImagemCelula 
      } 
     } 
    } 
    return cell 
} 

回答

0

首先,你必須創建一個新的數組,例如:

var images = [NSData]() 

或PFFile,UIImag e等...

var query = PFUser.query() 
    query!.whereKey("yourWherekey", equalTo: "your where value") 
    query?.findObjectsInBackgroundWithBlock{ 

     (results: [AnyObject]?, error) -> Void in 

     if results != nil { 

      for result in results! 
      { 
       self.images.append(result["image"] as! NSData) 

       self.tableView.reloadData() // important 


      } 

     } 
    } 

將圖像追加到圖像數組。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("cellA", forIndexPath: indexPath) as! UITableViewCell 

    cell.textLabel?.text = "TEST STRING" 
    cell.imageView?.image = UIImage(data: images[indexPath.row]) 

    return cell 
} 

並與indexPath.row連接。我用NSData編碼,如果你想你可以用PFFile代碼

+0

嗨Rys!對不起我遲到了。所以!我嘗試瞭解與Parse的這種關係,但我承認,不容易,至少對我來說,rsss我會把我的整個代碼嘗試和幫助我! –

+0

我更新了我的問題,並再次感謝RYS! –