从部分中包含多行的 tableView 中删除一行
Deleting a row from a tableView with multiple rows in section
当我尝试从包含多行的部分中删除一行时,当我向左滑动以显示删除按钮并按下它时,它会在刷新 tableView 后将其删除,但在视觉上它什么也没做。如果我再次按下它,它会删除该部分中的两行。如果我不再次按下它并按下返回然后返回到那个 viewController,则删除有效但动画从未发生。在只有一个单元格的部分中删除一个单元格效果很好。当该部分中有 2 个或更多单元格时,永远不会调用委托函数,但会调用 commitEditingStyle。让我知道您是否需要更多代码。谢谢
这是我的 NSFetchedResultsControllerDelegate 方法
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
self.context.deleteObject(object)
try! context.save()
}
func controllerWillChangeContent(controller: NSFetchedResultsController) {
}
func controllerDidChangeContent(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?)
{
var tableView = self.tableView as UITableView
switch(type)
{
case .Delete:
if let indexPath = indexPath
{
print("delete")
tableView.deleteRowsAtIndexPaths(NSArray(object:indexPath) as! [NSIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
break
case .Update:
if let indexPath = indexPath
{
if let cell = tableView.cellForRowAtIndexPath(indexPath)
{
cell.textLabel!.text = (self.fetchedResultsController.valueForKey("firstName") as! String) + " " + (self.fetchedResultsController.valueForKey("lastName") as! String)
print("update")
}
}
default:
break
}
}
func controller(controller: NSFetchedResultsController,
didChangeSection sectionInfo: NSFetchedResultsSectionInfo,
atIndex sectionIndex: Int,
forChangeType type: NSFetchedResultsChangeType)
{
switch(type)
{
case .Delete:
tableView.deleteSections(NSIndexSet(index:sectionIndex), withRowAnimation: UITableViewRowAnimation.Fade)
break
default:
break
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
}
解决方案:我需要在 commitEditingStyle 函数的末尾添加一个 self.tableView.reloadData()
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
self.context.deleteObject(object)
try! context.save()
self.tableView.reloadData()
}
删除该行后添加一个self.tableView.reloadData()
,或者该行在技术上已被删除,但table 视图尚未更新。请参阅下面的更新代码...
func controller(controller: NSFetchedResultsController,
didChangeSection sectionInfo: NSFetchedResultsSectionInfo,
atIndex sectionIndex: Int,
forChangeType type: NSFetchedResultsChangeType)
{
switch(type)
{
case .Delete:
//this creates the delete annimation
tableView.deleteSections(NSIndexSet(index:sectionIndex), withRowAnimation: UITableViewRowAnimation.Fade)
//this deletes the row from the array
self.array.removeAtIndex(indexPath.row)
//this reloads the table view so you can see the row deleted.
self.tableView.reloadData()
break
default:
break
}
}
当我尝试从包含多行的部分中删除一行时,当我向左滑动以显示删除按钮并按下它时,它会在刷新 tableView 后将其删除,但在视觉上它什么也没做。如果我再次按下它,它会删除该部分中的两行。如果我不再次按下它并按下返回然后返回到那个 viewController,则删除有效但动画从未发生。在只有一个单元格的部分中删除一个单元格效果很好。当该部分中有 2 个或更多单元格时,永远不会调用委托函数,但会调用 commitEditingStyle。让我知道您是否需要更多代码。谢谢
这是我的 NSFetchedResultsControllerDelegate 方法
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
self.context.deleteObject(object)
try! context.save()
}
func controllerWillChangeContent(controller: NSFetchedResultsController) {
}
func controllerDidChangeContent(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?)
{
var tableView = self.tableView as UITableView
switch(type)
{
case .Delete:
if let indexPath = indexPath
{
print("delete")
tableView.deleteRowsAtIndexPaths(NSArray(object:indexPath) as! [NSIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
break
case .Update:
if let indexPath = indexPath
{
if let cell = tableView.cellForRowAtIndexPath(indexPath)
{
cell.textLabel!.text = (self.fetchedResultsController.valueForKey("firstName") as! String) + " " + (self.fetchedResultsController.valueForKey("lastName") as! String)
print("update")
}
}
default:
break
}
}
func controller(controller: NSFetchedResultsController,
didChangeSection sectionInfo: NSFetchedResultsSectionInfo,
atIndex sectionIndex: Int,
forChangeType type: NSFetchedResultsChangeType)
{
switch(type)
{
case .Delete:
tableView.deleteSections(NSIndexSet(index:sectionIndex), withRowAnimation: UITableViewRowAnimation.Fade)
break
default:
break
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
}
解决方案:我需要在 commitEditingStyle 函数的末尾添加一个 self.tableView.reloadData()
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
self.context.deleteObject(object)
try! context.save()
self.tableView.reloadData()
}
删除该行后添加一个self.tableView.reloadData()
,或者该行在技术上已被删除,但table 视图尚未更新。请参阅下面的更新代码...
func controller(controller: NSFetchedResultsController,
didChangeSection sectionInfo: NSFetchedResultsSectionInfo,
atIndex sectionIndex: Int,
forChangeType type: NSFetchedResultsChangeType)
{
switch(type)
{
case .Delete:
//this creates the delete annimation
tableView.deleteSections(NSIndexSet(index:sectionIndex), withRowAnimation: UITableViewRowAnimation.Fade)
//this deletes the row from the array
self.array.removeAtIndex(indexPath.row)
//this reloads the table view so you can see the row deleted.
self.tableView.reloadData()
break
default:
break
}
}