从 tableView 中删除一行,在 section 中有多行

Posted

技术标签:

【中文标题】从 tableView 中删除一行,在 section 中有多行【英文标题】:Deleting a row from a tableView with multiple rows in 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()

【问题讨论】:

【参考方案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 次。但只有在最后一次删除时才会调用委托方法。 我想通了。在解决方案下查看下面的更新代码。但是您的建议使我得到了答案,所以谢谢。

以上是关于从 tableView 中删除一行,在 section 中有多行的主要内容,如果未能解决你的问题,请参考以下文章

以编程方式从带有动画的 customCell 类中的 tableView 中删除一行

从 tableview 部分删除最后一行时,iOS 应用程序崩溃

从 TableView 中的 CoreData 中删除实体时出错

在ios7中删除TableView的最后一行时出现动画问题

无法从自定义表格视图单元格按钮中删除行

tableview 删除部分中的最后一行