删除项目后更新 table:MGSwipeTableCell

Updating table after deleting an item: MGSwipeTableCell

我正在 Xcode 7 中使用 Swift 7 进行项目并使用 MGSwipeTableCell。

当用户在 table 单元格上滑动 left/right 时,我希望该单元格从视图中删除,而不是从我的后端数据库中删除,并根据 left/right 设置动画滑动的方向。当我 运行 应用程序并测试它发回此错误的功能时:

'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of 
sections.  The number of sections contained in the table view after the update (1) 
must be equal to the number of sections contained in the table view before the 
update (1), plus or minus the number of sections inserted or 
deleted (0 inserted, 1 deleted).'

这是我的函数代码:

cell.rightButtons = [MGSwipeButton(title: "Skip!", backgroundColor: UIColor(red: 245/255, green: 245/255, blue: 245/255, alpha: 1.0), callback: {
            (sender: MGSwipeTableCell!) -> Bool in
            print("Convenience callback for Skip button!")
            
            let indexPath = self.tableView.indexPathForCell(sender)
            self.tableView.deleteSections(NSIndexSet(index: indexPath!.section), withRowAnimation: UITableViewRowAnimation.Left)
            
            return true
        })]
        
        cell.rightExpansion.buttonIndex = 0
        
        cell.leftButtons = [MGSwipeButton(title: "Apply!", icon: UIImage(named:"check.png"), backgroundColor: UIColor(red: 245/255, green: 245/255, blue: 245/255, alpha: 1.0), callback: {
            (sender: MGSwipeTableCell!) -> Bool in
            print("Convenience callback for Apply button!")
            let indexPath = self.tableView.indexPathForCell(sender)
            self.tableView.deleteSections(NSIndexSet(index: indexPath!.section), withRowAnimation: UITableViewRowAnimation.Right)
            
            return true
        })]
        
        cell.leftExpansion.buttonIndex = 0
        
        return cell

这是我认为导致问题的 numberOfRowsInSection 部分:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    
    return posts.count
        

}

我是否认为需要编辑以上部分才能使其正常工作?我尝试了几种不同的方法来解决这个问题,但运气不佳。谁能帮我解决这个问题?

提前致谢!

多亏了下面的评论,我才弄明白了

            self.posts.removeAtIndex(indexPath!.row)
            
            self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Right)

deleteSections… 替换为 tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)。在该行之前,从 posts.

中删除 post