gpt4 book ai didi

ios - 从部分中包含多行的 tableView 中删除一行

转载 作者:行者123 更新时间:2023-11-29 01:55:22 24 4
gpt4 key购买 nike

当我尝试从包含多行的部分中删除一行时,当我向左滑动以显示删除按钮并按下它时,它会在刷新 tableView 后将其删除,但在视觉上它什么也没做。如果我再次按下它,它会删除该部分中的两行。如果我不再按下它并按下返回,然后返回到那个 viewController,则删除有效但动画从未发生。在只有一个单元格的部分中删除一个单元格效果很好。当该部分中有 2 个或更多单元格时,永远不会调用委托(delegate)函数,但会调用 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(),或者行在技术上被删除,但 TableView 尚未更新。请参阅下面更新的代码...

        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
}
}

关于ios - 从部分中包含多行的 tableView 中删除一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30924938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com