【问题标题】:Difficulty retrieving Parse Object Field Values难以检索解析对象字段值
【发布时间】:2015-10-02 21:34:58
【问题描述】:

我能够从 Parse Core 检索我的“项目”对象,然后将“项目名称”值返回给我并打印到日志中。我认为这些值会更新我的 productNames 数组,然后将其显示在我的 TableView 中。我的原型 tableview 单元格在 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"]

【问题讨论】:

    标签: swift uitableview parse-platform


    【解决方案1】:

    你应该用这个方法刷新你的tableview;

    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()
        })
    

    【讨论】:

    • 添加了,也意识到函数命名不正确。修复后再次运行它,在我的 tableView 中仍然没有结果。
    • 使用 findObjectsInBackgroundWithBlock 闭包添加 self.tableView.reloadData() 方法。我编辑了我的答案,只是尝试复制粘贴我的代码。
    • 你是圣人!谢谢。
    猜你喜欢
    • 2013-05-10
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 2015-02-09
    • 1970-01-01
    相关资源
    最近更新 更多