我可以从Parse Core中检索我的“项目”对象,然后将“projectname”值返回给我并打印到日志中。我认为这些值会更新我的productNames数组,然后在我的TableView中显示。我的原型tableview单元格在DealCell类中定义,带有图像和标签。当我运行应用程序时,我的tableView是空的。我错过了什么?
我的TableView的课程:
final_data
我的打印输出很奇怪,它看起来像这样,但它成功地获得了项目名称:
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
}
答案 0 :(得分:0)
您应该使用此方法刷新您的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()
})