ios - 解析 TableViewCell 中的值未更新

标签 ios swift parse-platform

非常感谢任何可以帮助我的人,非常感谢!

我正在构建一个约会应用程序,我试图在解析查询后将我的匹配项加载到表中。预期结果是 TableView 包含匹配图像和匹配 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


}





}

当我尝试在 TableView 中包含两个附加标签来表示“姓名”和“年龄”时,问题就出现了 - 我似乎无法找出在 Parse 的查询中调用它们以及工作照片查询的正确方法。

我想要的结果是表中的每个单元格都有一个图像(代码正在工作)ID(代码正在工作)名称(代码不工作)和年龄(代码不工作)

我所说的“不起作用”是指当我尝试从解析数据创建年龄变量时遇到大量错误,以便我可以将其传递到数组中,以便我的表格 View 可以显示旁边的文本图片。

这是我一直在“年龄”标签上使用的非工作代码,我相信错误是我试图使用“=数据”提取姓名/年龄,而我必须使用不同的术语?

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


}





}

最佳答案

您正在循环中重新加载表格 View (很多),而且当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
        }
}

关于ios - 解析 TableViewCell 中的值未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45197433/

相关文章:

arrays - 针对一系列范围测试序列中每个数字的最有效方法是什么?

ios - 如何在我发布的应用程序 Xcode 6 中上传图片和事件

ios - 从 objective-c 调用 swift 代码时,第一次推送 View Controller 花费了太多时间

swift - 从 swift 设置 char[] 到 c 结构中

java - Parse.com 查询太慢,需要更快

java - 无法使用 ParseObject 解析 Java.lang.NullPointerException

ios - 将媒体流添加到对等连接 (WebRTC) 时应用程序崩溃

ios - 如何在 Swift (iOS 7) 中向 UIAlertView 添加 Action

ios - 字母表怎么放?

android - 解析推送通知 android 设备 token 在某些情况下未保存