我正在使用Swift 7和使用MGSwipeTableCell在Xcode 7中开展项目。
当用户在表格单元格上向左/向右滑动时,我希望从视图中删除该单元格,而不是从我的后端数据库中删除该单元格,并根据滑动的方向向左/向右动画。当我运行应用程序并测试该功能时,它会发回这个错误:
'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)
答案 0 :(得分:0)
将deleteSections…
替换为tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
。直接在该行之前,从posts
删除帖子。