【发布时间】:2016-01-24 07:25:00
【问题描述】:
我有一个从 Parse 数据库中提取信息并将其显示在 UITableView 中的应用程序。我从 viewWillAppear 函数中的解析中提取信息,并将其显示在 tableView(cellForRowAtIndexPath) 函数中。有时我收到一个错误,因为存储 Parse 信息的数组的长度为 0,我尝试访问数组边界之外的索引处的信息。我相信这是因为在 viewWillAppear 完成运行之前调用了 cellForRowAtIndexPath。这是可能的还是我的错误肯定来自其他地方?
编辑:错误不会每次都发生,我找不到重现它的方法
override func viewWillAppear(animated: Bool) {
//begin ignoring events until the information is finished being pulled
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
resultsArray.removeAll(keepCapacity: false)
//run query
let query = PFQuery(className: "Answers")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let objects = objects {
//append information to the resultsArray
}
}
self.tableView.reloadData()
}
}
//information is now pulled, so allow interaction
UIApplication.sharedApplication().endIgnoringInteractionEvents()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! answerCell
// THIS IS WHERE THE ERROR OCCURS
resultsArray[indexPath.row].imageFile.getDataInBackgroundWithBlock { (data, error) -> Void in
//set image within cell
}
return cell
}
【问题讨论】:
-
从你身边放一些代码
-
请看代码@Graham
-
请在
cellForRowAtIndexPath中设置一个断点,然后运行并逐步查看发生了什么。然后你可以同时看到你的 arry 是否为空。 -
你能显示
numberOfRowsInSection的代码吗? -
numberOfRowsInSection 返回 resultsArray.count @Paulw11
标签: ios xcode swift uitableview parse-platform