2017-07-19 58 views
0

非常感謝所有能夠幫助我的人,非常感謝!TableViewCell中的解析值不會更新

我正在構建一個約會應用程序,我試圖讓我的匹配在解析查詢後加載到表中。預期的結果是表格視圖包含匹配圖像和匹配ID。現在我的代碼完全可以在下面工作。

import UIKit 
import Parse 


class MyListViewController: UIViewController, UITableViewDataSource, 
UITableViewDelegate { 

var images = [UIImage]() 
var userIds = [String]() 

@IBOutlet weak var tView: UITableView! 

@IBAction func toSwiperButton(_ sender: Any) { 


    performSegue(withIdentifier: "ToSwiper", sender: self) 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let query = PFUser.query() 

    query?.whereKey("objectId", containedIn: PFUser.current()?["accepted"] 
as! [String]) 

    query?.findObjectsInBackground(block: { (objects, error) in 

     if let users = objects { 

      for object in users { 

       if let user = object as? PFUser { 

        let imageFile = user["photo"] as! PFFile 

        imageFile.getDataInBackground(block: { (data, error) in 

         if let imageData = data { 

          self.images.append(UIImage(data: imageData)!) 

          self.userIds.append(user.objectId!) 

          self.tView.reloadData() 


         } 

        }) 
       } 

      } 

     } 

    }) 
} 

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


internal func tableView(_ tableView: UITableView, numberOfRowsInSection 
section: Int) -> Int { 

return images.count 
} 

internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! PDPLIstViewTableViewCell 

cell.Image.image = images[indexPath.row] 

cell.id.text = userIds[indexPath.row] 

return cell 


} 





} 

當我嘗試包括兩個附加標籤上的「名稱」表視圖和「年齡」的問題出現了 - 我似乎無法弄清楚正確的方式來調用它們在解析查詢一起工作照片查詢。

我想要的結果是爲表中的每一個細胞都具有的圖像(代碼工作)ID(代碼工作)名稱(代碼不工作),年齡(代碼不工作)

通過「不工作「我的意思是,當我嘗試從解析數據創建年齡變量時,出現大量錯誤,因此我可以將其傳遞到數組中,以便我的tableview可以顯示圖像旁邊的文本。

這是我一直用於「年齡」標籤上的非工作代碼,我相信錯誤是我試圖用「= data」拉名稱/年齡的地方,我必須使用不同的術語?

import UIKit 
import Parse 


class MyListViewController: UIViewController, UITableViewDataSource, 
UITableViewDelegate { 

var images = [UIImage]() 
var userIds = [String]() 
var name = [String]() 
var age = [String]() 

@IBOutlet weak var tView: UITableView! 

@IBAction func toSwiperButton(_ sender: Any) { 


    performSegue(withIdentifier: "ToSwiper", sender: self) 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let query = PFUser.query() 

    query?.whereKey("objectId", containedIn: PFUser.current()?["accepted"] 
    as! [String]) 

    query?.findObjectsInBackground(block: { (objects, error) in 


     if let users = objects { 

      for object in users { 

       if let user = object as? PFUser { 

        let ageFile = user["age"] as! PFFile 

        ageFile.getDataInBackground(block: { (data, error) in 

         if let ageData = data { 

          self.age.append(UILabel(data: ageData)!) 
         } 

        let imageFile = user["photo"] as! PFFile 

        imageFile.getDataInBackground(block: { (data, error) in 

         if let imageData = data { 

          self.images.append(UIImage(data: imageData)!) 

          self.userIds.append(user.objectId!) 

          self.age.append(String(data: ageFile)) 

          self.tView.reloadData() 


         } 

        }) 
       } 

      } 

     } 

    }) 
} 

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


internal func tableView(_ tableView: UITableView, numberOfRowsInSection 
section: Int) -> Int { 

    return images.count 
} 

internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: 
IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: 
indexPath) as! PDPLIstViewTableViewCell 

    cell.image.image = images[indexPath.row] 

    cell.id.text = userIds[indexPath.row] 

    cell.name.text = name[indexPath.row] 

    cell.age.text = age[indexPath.row] 

    return cell 


} 





} 

回答

0

您正在重新加載循環中的tableview(很多),並且當ageData完成時您不重新加載。一旦查詢完成,嘗試重新加載tableview一次。在:

internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: 
IndexPath) -> UITableViewCell { 

    guard let ageFile = age[indexPath.row] as? PFFile else { return } 

    ageFile.getDataInBackground(block: { (data, error) in 

     if let ageData = data { 

      cell.age.text = ageData 
     } 
} 
+0

這是一個巨大的幫助,指向我在正確的方向,我最終在一個不同的地方位,但非常感謝你指出正確的方式。非常感謝迴應! –

+0

當然,沒問題。如果答案是正確的,你可以將這個答案作爲正確答案。否則,爲未來的用戶回答你自己的問題可能會很好。 – Lengo

+0

我沒有投票它是正確的,但系統說我的選票沒有顯示,因爲我沒有足夠的聲譽點whop whop ... –