UITableView, вызывающий метод deleteRow (), потерпел крах из-за отсутствия заполнителя для пути индекса - PullRequest
0 голосов
/ 31 августа 2018

Вот мой код для редактирования 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. И приложение упало при удалении строки 1 (раздел 2) в «tableView.deleteRows ()» Консоль:

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

Удаление строки

Что я должен сделать, чтобы решить эту проблему?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...