swift - 难以检索解析对象字段值

标签 swift uitableview parse-platform

我能够从 Parse Core 检索我的“项目”对象,然后将“项目名称”值返回给我并打印到日志中。我认为这些值会更新我的 ProductNames 数组,然后该数组将显示在我的 TableView 中。我的原型(prototype)表格 View 单元格在 DealCell 类中定义,带有图像和标签。当我运行应用程序时,我的 tableView 是空的。我错过了什么?

我的TableView的类:

var productIds = [String]()
var productNames = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    self.productIds.removeAll(keepCapacity: true)
    self.productNames.removeAll(keepCapacity: true)

    // Ask for the current user's personal list of saved PFObject IDs.
    let Ids = PFUser.currentUser()?.objectForKey("accepted") as! NSArray
    print(Ids)

    // Requesting the Project objects based on the Ids retrieved.
    let projectRetrieval = PFQuery(className: "project")
    projectRetrieval.whereKey("objectId", containedIn: Ids as [AnyObject])
    projectRetrieval.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
        if let objects = objects {
            for object in objects {

                //Append the productNames array with retrieved projectnames
                self.productNames.append(object["projectname"] as! String)

                // This line successfully prints the array of product Names retrieved.
                print("Projectnames: \(self.productNames)")

            }
        }
    })

}

// Number of Rows
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.productNames.count;
}

// Assign the productNames to the labels in each cell.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.tableView.dequeueReusableCellWithIdentifier("dealcell", forIndexPath: indexPath) as! DealCell

    cell.productName.text = self.productNames[indexPath.row] 

    return cell

}

我的打印输出很奇怪,它看起来像这样,但它成功获取了项目名称:

Projectnames: ["Personal Drone"]
Projectnames: ["Personal Drone", "Prosthetic Limb Patent"]

最佳答案

您应该使用此方法刷新您的桌面 View ;

self.tableView.reloadData()

您可以将此代码放在您的案例中的 findObjectsInBackgroundWithBlock 回调闭包之后;

projectRetrieval.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
        if let objects = objects {
            for object in objects {

                //Append the productNames array with retrieved projectnames
                self.productNames.append(object["projectname"] as! String)

                // This line successfully prints the array of product Names retrieved.
                print("Projectnames: \(self.productNames)")

            }
        }
       self.tableView.reloadData()
    })

关于swift - 难以检索解析对象字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32916193/

相关文章:

ios - 重新加载表格 View 后出现抖动

ios - 我如何在输出中得不到可选?

swift - ios12系统图像按钮的色调颜色不起作用

JavaFX FXML Controller ClassNotFoundException

swift - 为什么 WKWebView 变成了 nil?

swift - 我怎样才能修剪我的 UITableViewController 以便它始终只显示最后 10 行?

ios - 解析 : User and error both nil with logInInBackgroundWithReadPermissions()

swift - 无法在一个 View 单元格中使用两个不同的 TableView 单元格

ios - 使用swift在图像上叠加文字

ios - SWIFT:使用 UIWebkit View