【发布时间】:2015-05-08 06:26:14
【问题描述】:
想不出这个问题的好名字...如果你能想到更好的名字,请随时编辑:)
我正在使用 Swift 和 Parse.com 构建 iOS 应用。
在我的应用程序中,我有一个主要的PFQueryTableViewController,它将我的 Parse 云中的一些数据加载到一些自定义 UITableViewCells 中。
我想要的单元格标签值之一需要一段时间 Parse 才能返回,因此我使用 findObjectsInBackgroundWithBlock() 获取它。
在我的cellForRowAtIndexPath 中,当我加载我的表格时,我有以下代码:
// Set cells for each row of table
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
var cell: CustomTableViewCell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! CustomTableViewCell
// Get Course Object objectID to hand to getCourseGrade function
var anObjectID: String = object!.objectId!
cell.setCell(name: object!["objectName"] as! String, code: object!["objectCode"] as! String, grade: getObjectGrade(anObjectID))
return cell
}
在上面的代码中,我调用了一个名为getObjectGrade 的函数来将一个值传递给我的setCell() 函数,该函数在构建UITableView 时设置customTableViewCells,该UITableView 运行如下(简化):
func getObjectGrade(objectIdString: String) -> Float {
// Set a starting value of objectGrade
var objectGrade: Float = -1
//...I set up a PFQuery
query?.findObjectsInBackgroundWithBlock({ (objects: [AnyObject]?, error: NSError?) -> Void in
//...here I am retrive the value I need from Parse --> valueFromParse
objectGrade = valueFromParse
})
return objectGrade
}
现在,我非常清楚这将不起作用...显然代码不会等待我的 findObjectsInBackgroundWithBlock() 代码运行,因此在更新之前返回 objectGrade .
我的问题:一旦findObjectsInBackgroundWithBlock() 代码部分完成,我如何设置单元格标签的值?
【问题讨论】:
-
@Kutyel,这个问题完全不同。我从 Parse 获取数据没有问题,我不确定如何在异步函数之后设置自定义单元格的标签值。关键还在于我需要维护对单元格的引用......这是棘手的部分。
-
如果你在后台做一个进程,那么不要依赖return语句。我相信这个代码
block会在下载完成后执行,所以调用另一个可以触发单元格更新的辅助方法并在主线程上执行(在解析中找到一个等价)。 -
是的,甘道夫,谢谢 :) 现在解决了!谢谢
标签: ios uitableview swift parse-platform