删除 tableView 中的项目会从存储的值中删除错误的对应文件(swift NSCoding) [英] Deleting an item in tableView removes the wrong corresponding file from stored values (swift NSCoding)

查看:14
本文介绍了删除 tableView 中的项目会从存储的值中删除错误的对应文件(swift NSCoding)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码从 TableView 中删除了一个项目,并从存档中删除了相应的文件.如果我删除 TableView 的第 4 个项目,它会删除存储值的第 4 个文件.由于 TableView 是动态的(排序项目或更改它显示的项目数量)并且存档稳定,代码会删除错误的文件,因为视图中的第一个项目可能是存储文件中的第 6 个.所以当视图重新加载时,一个项目被删除,但不是我想要的.(我使用 NSCoding 来存储值).每个项目都有一个唯一的 id,每个文件都有一个唯一的名称(即 id),但我只能检索真正删除的项目的 id,而不是我滑动删除的那个.请你帮助我好吗?谢谢.

I used the code below to delete an item from a TableView and the corresponding file from the archive. If I delete the 4th item of the TableView, it deletes the 4th file of the stored values. As the TableView is dynamic(sorting items or changing the number of items that it displays)and the archive is stable, the code deletes the wrong files, as the 1st item in the view could be the 6th in the stored Files. So when the view is reloaded an item is deleted but not the one I wanted. (I used NSCoding to store values). Every item has a unique id and every file a unique name(which is the id), but I can only retrieve the id of the truly deleted item and not the one I swipe to delete. Could you please help me? Thank you.

self.tableOfExpences.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
        
//REMOVING FROM THE STORED VALUES================         
        
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
 let paths:[String] = Bundle.paths(forResourcesOfType: "dat", inDirectory: documentsPath)
        if paths.count > 0 {
             do {
                try FileManager.default.removeItem(atPath: paths[indexPath.row])
                print("deleted File has been removed")
                } catch {
                print("an error during a removing")
                }
        }
        
   
                
    } else if editingStyle == .insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}

尝试解决可能的同步问题

Trying to solve possible sync problem

首先,我确保表格视图知道它显示的内容.看来阵列已同步.在调试区我读到:

First, I make sure the table view knows what it shows. And it appears the arrays are synced. In the debug area I read:

第一个单元格是 Optional("Chocolate") 并具有 ID Optional(6)

the first cell is the Optional("Chocolate") and has the ID Optional(6)

第二个单元格是 Optional("phone") 并且具有 ID Optional(7)

the second cell is the Optional("phone") and has the ID Optional(7)

以上都是正确的.

然后我滑动以移除带有 ID 7 的第二个手机(电话),它通知我将移除具有另一个 ID 和另一条路径的完全不同的费用.

Then I swipe to remove the second cell (the phone) with ID 7 and it informs me that a totally different expense with another ID and another path will be removed.

对于上述问题,解决方案是 [indexPath.row-1] 而不是 [indexPath.row].

For the above problem the solution came with [indexPath.row-1] instead of [indexPath.row].

     if indexPath.row == 0 {
            
            self.tableOfExpences.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
            
        } else {
        self.tableOfExpences.remove(at: indexPath.row-1)
        tableView.deleteRows(at: [indexPath], with: .fade)
        }

上述解决方案有效,但当我删除最后一项时,应用程序崩溃了.所以在看了类似的问题之后,解决方案是回到 [indexPath.row] 而不是 [indexPath.row-1] 并且只是颠倒行的顺序,以便首先从文件中删除数据

The above solution worked but when I deleted the last item the app crashed. So after watching similar Questions the solution was to go back to [indexPath.row] instead of [indexPath.row-1] and just reverse the order of lines so that first to delete data from the file

    try FileManager.default.removeItem(atPath: paths[indexPath.row])

然后执行

   self.tableOfExpences.remove(at: indexPath.row) 

我希望以上内容能帮助遇到类似问题的人.当然如果没有filePth属性和同步调试,我仍然会看到代码崩溃.感谢您的大力帮助.这是美妙的一周.

I hope that the above will help someone with similar problems.Of course without the filePth property and the sync debuging I would still watch the code crash.Thank you for the great help. It was a wonderfull week.

推荐答案

该问题很可能是由于您的两个阵列不同步造成的.

The issue is likely being caused by your two arrays getting out of sync.

当您通过获取 Documents 目录中的文档来获取路径数组时,您无法保证它们会以与表数据相同的排序顺序返回.

When you get the paths array by fetching the documents in the Documents directory you have no guarantee that they will be returned in teh same sorted order that your table data is.

您也可以改为添加费用模型的路径,这样会更可靠

You could also add the path to the expenses model instead, which would be much more reliable

这篇关于删除 tableView 中的项目会从存储的值中删除错误的对应文件(swift NSCoding)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆