【问题标题】:Deleting a row from a tableView with multiple rows in section从 tableView 中删除一行,在 section 中有多行
【发布时间】:2015-06-18 20:12:34
【问题描述】:

当我尝试从具有多行的部分中删除一行时,当我向左滑动以显示删除按钮并按下它时,它会在刷新 tableView 后将其删除,但在视觉上它什么也不做。如果我再次按下它,它会删除该部分中的两行。如果我不再按它并按回然后返回到该视图控制器,则删除已起作用,但动画从未发生。在只有一个单元格的部分中删除一个单元格效果很好。当该部分中有 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()
}

【问题讨论】:

    标签: ios swift tableview nsfetchedresultscontroller


    【解决方案1】:

    删除行后添加self.tableView.reloadData(),或者技术上该行已删除,但表视图尚未更新。请参阅下面的更新代码...

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

    【讨论】:

    • 问题是这些函数永远不会在第一次尝试删除时被调用。如果该部分中有 2 行,我必须按两次删除,然后两行都被删除。如果有 3 行,我必须按 3 次。但只有在最后一次删除时才会调用委托方法。
    • 我想通了。在解决方案下查看下面的更新代码。但是您的建议使我得到了答案,所以谢谢。
    猜你喜欢
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    相关资源
    最近更新 更多