Ios UITableView调用deleteRow()方法因缺少索引路径的占位符而崩溃
以下是我编辑tableView的代码:Ios UITableView调用deleteRow()方法因缺少索引路径的占位符而崩溃,ios,swift,cocoa-touch,Ios,Swift,Cocoa Touch,以下是我编辑tableView的代码: override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == UITableViewCellEditingStyle.delete { plistContentArray = readFile(
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
plistContentArray = readFile()
let section : NSInteger = indexPath.section
let row : NSInteger = indexPath.row
var sectionDictionary:NSMutableDictionary = plistContentArray.object(at: section) as! NSMutableDictionary
var contentArray = sectionDictionary.object(forKey: "Content")
if contentArray is String {
print("String")
plistContentArray.removeObject(at: section)
plistContentArray.write(toFile: plistPath, atomically: true)
let sectionPosition = section
let sectionIndexSet = IndexSet(integer: sectionPosition)
self.tableView.deleteSections(sectionIndexSet, with: .fade)
} else {
let valueToDelete = (contentArray as! NSArray).object(at: row) as! String
let originalValue = contentArray as! [String]
let filteredValue = originalValue.filter{ $0 != valueToDelete }
sectionDictionary.setValue(filteredValue, forKey: "Content")
plistContentArray[section] = sectionDictionary
print(plistContentArray)
plistContentArray.write(toFile: plistPath, atomically: true)
let sectionPosition = section
let sectionIndexSet = IndexSet(integer: sectionPosition)
if filteredValue.count == 0 {
plistContentArray.removeObject(at: section)
plistContentArray.write(toFile: plistPath, atomically: true)
self.tableView.deleteSections(sectionIndexSet, with: .fade)
} else {
tableView.beginUpdates()
let indexPathToDelete = IndexPath(row: row, section: section)
self.tableView.deleteRows(at: [indexPathToDelete], with: .automatic)
tableView.endUpdates()
}
}
}
}
readFile()方法是我用来从plist文件获取数据的方法。
当在“tableView.deleteRows()中删除第1行(第2节)时,应用程序崩溃
控制台:
***由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:“UITableView内部不一致:此索引路径缺少占位符上下文:{length=2,path=2-1}”
我应该怎么做才能解决这个问题?我认为您没有第2节在Swift中使用
NSMutable…
集合类型。您不能将对象强制转换为NSMutable…
类(甚至在Objective-C中也不能)。你永远不会得到一个可变的对象。另一个非常糟糕的objectice-c-ish代码是NSInteger
,值(forKey/setValue(FrKEY < /COD>和编写<代码> NS…< /COD>基础集合类型为属性列表到磁盘。不要在SWIFT中这样做。有<代码> int <代码>、密钥订阅和专用API >代码>属性列表序列化< /代码>以序列化属性列表。和<代码>开始更新/ EntDebug < /C>对单个插入/删除/移动没有任何影响。活动
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView internal inconsistency: missing placeholder context for this index path: <NSIndexPath: 0xc000000000200216> {length = 2, path = 2 - 1}'