Ios 无法删除UITableView中的行
我有两个数组,在删除行时需要从中删除数据,这两个数组似乎都已删除。但是,当我滑动以删除时,会出现以下错误: [2078:568325]***在-[UITableView]中的断言失败 _endCellAnimationsWithContext:,/SourceCache/UIKit/UIKit-3318.16.21/UITableView.m:1582[2078:568325] ***由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:无效” 节0中的行数。表中包含的行数 更新2后的现有节数必须等于 更新2之前该节中包含的行,加号或减号 从插入的节0插入或删除的行数, 1删除并加上或减去移入或移出的行数 第0节搬进来了,第0节搬出去了。”Ios 无法删除UITableView中的行,ios,xcode,swift,Ios,Xcode,Swift,我有两个数组,在删除行时需要从中删除数据,这两个数组似乎都已删除。但是,当我滑动以删除时,会出现以下错误: [2078:568325]***在-[UITableView]中的断言失败 _endCellAnimationsWithContext:,/SourceCache/UIKit/UIKit-3318.16.21/UITableView.m:1582[2078:568325] ***由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更
它似乎在deleteRowsAtIndexPaths上出错 在从数据模型中删除元素之前,请尝试移动调用以删除行。大概是这样的:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
audioPlayers[indexPath.row].stop()
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
audioFiles.removeAtIndex(indexPath.row)
audioPlayers.removeAtIndex(indexPath.row)
}
}
我让它像
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
managedObjectContext.deleteObject(fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject)
do {
try managedObjectContext.save()
} catch {
print("error managed object context in dictionary table")
}
} else if editingStyle == .Insert {
}
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case NSFetchedResultsChangeType.Insert:
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case NSFetchedResultsChangeType.Delete:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
case NSFetchedResultsChangeType.Move:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case NSFetchedResultsChangeType.Update:
tableView.cellForRowAtIndexPath(indexPath!)
}
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch type {
case NSFetchedResultsChangeType.Insert:
tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case NSFetchedResultsChangeType.Delete:
tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case NSFetchedResultsChangeType.Move:
tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case NSFetchedResultsChangeType.Update:
break
}
}
表中的行数返回什么值view@chrono删除数据后,需要重新加载tableView。最后,请尝试[tableView reloadData]返回enumeratedDocumentDirectory函数的计数,该函数返回strings@Akshay我把它扔进去了,但它还是扔了同样的错误。我把它放在removeAtIndex和tableView之间。deleteRows@chrono尝试将其放在删除和调试该部分中的代码之后,因为更新表视图和删除itemNegatory没有意义。谢谢你的建议。似乎只是没有更新我的文档模型中的行。您的意思是应用我的解决方案后,您看不到您的数据模型音频文件和音频播放器更新吗?
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
managedObjectContext.deleteObject(fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject)
do {
try managedObjectContext.save()
} catch {
print("error managed object context in dictionary table")
}
} else if editingStyle == .Insert {
}
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case NSFetchedResultsChangeType.Insert:
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case NSFetchedResultsChangeType.Delete:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
case NSFetchedResultsChangeType.Move:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case NSFetchedResultsChangeType.Update:
tableView.cellForRowAtIndexPath(indexPath!)
}
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch type {
case NSFetchedResultsChangeType.Insert:
tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case NSFetchedResultsChangeType.Delete:
tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case NSFetchedResultsChangeType.Move:
tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case NSFetchedResultsChangeType.Update:
break
}
}