我想从表格视图中删除一个项目。
问题已解决,但我仍然删除了错误的文件,因此我检查了数组的同步。
首先,我确保表视图知道它显示的内容,并且看起来阵列已同步。我调试代码,我发现当我滑动删除项目时代码仍然删除另一个文件。
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)
解决了最后一个大问题。