Я хочу удалить элемент из табличного представления.
- Сначала я добавил свойство filePath в свой класс (которого не было)
- Затем я исправил строку кода, чтобы найти правильный путь к файлу
проблема решена, но все-таки у меня удален неправильный файл, поэтому я проверяю синхронизацию массивов.
Во-первых, я проверяю, что табличное представление знает, что оно показывает, и кажется, что массивы синхронизированы. Я отлаживаю код и обнаруживаю, что когда я провожу пальцем, чтобы удалить элемент, код все равно удаляет другой файл.
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableOfExpences.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellOfExpencesTableViewCell
let myOneExpense = self.tableOfExpences[indexPath.row]
if myOneExpense.size != nil {
//Arrangement Of the cell
//allocate the right image for each segment
} else {
cell.nameCellLabel?.text = "No Cost"
}
return cell
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
self.tableOfExpences.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
print("the removed from the table expence is the \(String(describing: tableOfExpences[indexPath.row].expenseID))")
print("We had bought a \(String(describing: tableOfExpences[indexPath.row].note))")
В моем случае было множество причин, по которым неправильный файл был удален, но самым хитрым (когда весь код казался правильным) было то, что сначала файл должен быть удален из каталога, а затем ячейка из таблицы.
Так что просто поменяйте местами порядок строк, чтобы сначала удалить данные из файла
try FileManager.default.removeItem(atPath: paths[indexPath.row])
и затем реализовать
self.tableOfExpences.remove(at: indexPath.row)
решил последнюю большую проблему.