我有一个动态的 TableView,想用动画删除行。
我的代码:
struct TableItem {
let text: String
let id: String
let creationDate: String
let bug: String
let comments: String
}
var sections = Dictionary<String, Array<TableItem>>()
var sortedSections = [String]()
//Some other Code
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let setChecked = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Erledigt" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
var tableSection = self.sections[self.sortedSections[indexPath.section]]
let tableItem = tableSection![indexPath.row]
//Not important HTTP POST Code
if(success == 1)
{
NSLog("SUCCESS");
self.tableView.beginUpdates()
tableSection?.removeAtIndex(indexPath.row)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Left)
self.tableView.endUpdates()
//self.getChecked()
}
}
return [setChecked]
}
如果我运行此代码,我会收到以下错误消息:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 6 节中的行数无效。更新 (2) 后现有部分中包含的行数必须等于行数在更新 (2) 之前包含在该部分中,加上或减去从该部分插入或删除的行数(0 插入,1 删除)以及加上或减去移入或移出该部分的行数(0 移入, 0 移出)。'
我不知道我做错了什么。
感谢您的帮助。
最佳答案
这就是值类型陷阱。
Swift 集合类型是具有值语义的结构,与具有引用语义的类不同。
var tableSection = self.sections[self.sortedSections[indexPath.section]]
行复制了对象并保留 self.sections
不变。
从列表中删除项目后,您必须将数组分配回 self.sections
。
tableSection?.removeAtIndex(indexPath.row)
self.sections[self.sortedSections[indexPath.section]] = tableSection
关于ios - swift TableView deleteRowsAtIndexPaths NSException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39817292/