2017-07-19 32 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 ... –