0
删除行,我需要一些帮助,这代码:CoreData - 从的tableView
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
println("section and row \(indexPath.section) \(indexPath.row) ")
if self.fetchedResultsController == nil {
println("error when trying to delete object from managed object")
} else if (editingStyle == UITableViewCellEditingStyle.Delete) {
moc?.deleteObject(detailsArray[indexPath.row] as NSManagedObject)
detailsArray.removeAtIndex(indexPath.row)
var error: NSError?
moc?.save(&error)
}
}
// MARK: NSFetchedResultsControllerDelegate
func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.exerciseTableView.beginUpdates()
}
func controller(controller: NSFetchedResultsController,
didChangeObject anObject: AnyObject,
atIndexPath indexPath: NSIndexPath?,
forChangeType type: NSFetchedResultsChangeType,
newIndexPath: NSIndexPath?)
{
switch type {
case NSFetchedResultsChangeType.Insert:
// Note that for Insert, we insert a row at the __newIndexPath__
if let insertIndexPath = newIndexPath {
self.exerciseTableView.insertRowsAtIndexPaths([insertIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
case NSFetchedResultsChangeType.Delete:
// Note that for Delete, we delete the row at __indexPath__
if let deleteIndexPath = indexPath {
self.exerciseTableView.deleteRowsAtIndexPaths([deleteIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
case NSFetchedResultsChangeType.Update:
// Note that for Update, we update the row at __indexPath__
if let updateIndexPath = indexPath {
let cell = self.exerciseTableView.cellForRowAtIndexPath(updateIndexPath)
let details = self.fetchedResultsController!.objectAtIndexPath(updateIndexPath) as? TrainingDetails
cell!.textLabel!.text = "\(details!.exerciseName)"
cell!.detailTextLabel!.text = "Sets: #\(details!.setsNumber) Reps: #\(details!.repsNumber)"
}
case NSFetchedResultsChangeType.Move:
// Note that for Move, we delete the row at __indexPath__
if let deleteIndexPath = indexPath {
self.exerciseTableView.deleteRowsAtIndexPaths([deleteIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
// Note that for Move, we insert a row at the __newIndexPath__
if let insertIndexPath = newIndexPath {
self.exerciseTableView.insertRowsAtIndexPaths([insertIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
} }
func controller(controller: NSFetchedResultsController,
didChangeSection sectionInfo: NSFetchedResultsSectionInfo,
atIndex sectionIndex: Int,
forChangeType type: NSFetchedResultsChangeType)
{
switch type {
case .Insert:
let sectionIndexSet = NSIndexSet(index: sectionIndex)
self.exerciseTableView.insertSections(sectionIndexSet, withRowAnimation: UITableViewRowAnimation.Fade)
case .Delete:
let sectionIndexSet = NSIndexSet(index: sectionIndex)
self.exerciseTableView.deleteSections(sectionIndexSet, withRowAnimation: UITableViewRowAnimation.Fade)
default:
""
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
exerciseTableView.endUpdates()
}
}
所以,问题是每当我试图从表中删除视图的东西,它只是正常运行,如果它的第一项表视图。如果我尝试删除不是第一个的其他项目,则应用程序崩溃并且它说阵列索引超出范围。
我不明白我的错误在哪里。有人能帮我吗?
我希望我们能破解这个!提前致谢。
UPDATE
的cellForRowAtIndexPath功能:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("exerciseCell", forIndexPath: indexPath) as! UITableViewCell
let details = fetchedResultsController!.objectAtIndexPath(indexPath) as! TrainingDetails
cell.textLabel!.text = "\(details.exerciseName)"
cell.detailTextLabel!.text = "Sets: #\(details.setsNumber) Reps: #\(details.repsNumber)"
return cell
}
我不知道发生了什么,但现在它崩溃在这行** moc?.deleteObject(detailsArray [indexPath.row]作为NSManagedObject)**说数组索引超出范围! –
它应该工作,不是吗? –
可以为deleteObject函数添加代码吗?您是否在使用NSPredicate删除特定数据? – Karlos