我有一个应用程序从Parse数据库中提取信息,并将其显示在UITableView中。我从viewWillAppear函数中解析信息,并将其显示在tableView(cellForRowAtIndexPath)函数中。有时候我收到一个错误,因为存储Parse信息的数组的长度为0,我尝试访问数组边界之外的索引处的信息。我相信这是因为在viewWillAppear完成运行之前,cellForRowAtIndexPath被调用。这是可能的还是我的错误肯定来自其他地方?在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 – rohaldb
请把一个破发点中的'cellForRowAtIndexPath'然后运行和走一步看一步是怎么回事。那么同一时间你可以看到你的arry是否为空。 –