我有一個應用程序從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是否爲空。 –