UITableView调用deleteRow()方法由于缺少索引路径的占位符而崩溃

时间:2018-08-31 06:18:11

标签: ios swift cocoa-touch

这是我编辑tableView的代码:

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节)时,应用程序崩溃了 控制台:

*** 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}'

Deleting Row

我该怎么做才能解决此问题?

0 个答案:

没有答案