重新排序 TableView 中的行将在数据刷新后重置为默认值

Reordering rows in TableView would reset to default after data refresh

我实现了一个 table 视图,它显示了一个文档列表

我用这段代码实现了重新排序功能

var documents = [PDFDocument]()
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {

    let movedObject = self.documents[sourceIndexPath.row]

    let fileManager = FileManager.default

    documents.remove(at: sourceIndexPath.row)

    documents.insert(movedObject, at: destinationIndexPath.row)

    NSLog("%@", "\(sourceIndexPath.row) => \(destinationIndexPath.row) \(documents)")



    do {


        let document = documents[sourceIndexPath.row]
        let documentURL = document.documentURL?.absoluteString


        let document_Dest = documents[destinationIndexPath.row]
        let documentURL_Dest = document_Dest.documentURL?.absoluteString

        try fileManager.moveItem(atPath: documentURL!, toPath: documentURL_Dest!)
    }
    catch let error as NSError {
        print("Ooops! Something went wrong: \(error)")
    }




    // To check for correctness enable: self.tableView.reloadData()
   refreshData()

}


  private func refreshData() {
    let fileManager = FileManager.default
    let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let contents = try! fileManager.contentsOfDirectory(at: documentDirectory, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
    documents = contents.flatMap { PDFDocument(url: [=10=]) }

    tableView.reloadData()
}

它在重新排序时工作得很好,但问题是当我关闭应用程序并再次启动它时(任何时候刷新数据功能运行),行返回到它们的默认位置。而我需要在重新排序时保存更改。

tableView 中的重新排序仅特定于 tableView。这种重新排序不会影响原始数据源。关闭应用程序并再次 运行 重新加载数据的方法从文档目录中获取原始数据。当此顺序在 tableView 中更改时,您需要更改此原始文件的顺序。

  1. 从文档目录获取数据。

    private func refreshData() 
    {
        let fileManager = FileManager.default
        let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let contents = try! fileManager.contentsOfDirectory(at: documentDirectory, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
        documents = contents.flatMap { PDFDocument(url: [=10=]) }
    }
    
  2. 创建一个字典,键为文档目录文件或文件夹名称,值为索引。

    private func refreshData() 
    {
        let fileManager = FileManager.default
        let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let contents = try! fileManager.contentsOfDirectory(at: documentDirectory, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
        documents = contents.flatMap { PDFDocument(url: [=11=]) 
    
    
        let index = 0
        let documentDic = NSMutableDictionary()
        for element in documents
        {
             documentDic.setValue(index, forKey: element.documentURL?.absoluteString) 
             index = index + 1
        }  
    
    }
    
  3. 将其保存在用户默认值中

    private func refreshData() 
    {
        let fileManager = FileManager.default
        let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let contents = try! fileManager.contentsOfDirectory(at: documentDirectory, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
        documents = contents.flatMap { PDFDocument(url: [=12=]) 
    
        let index = 0
        let documentDic = NSMutableDictionary()
        for element in documents
        {
             documentDic.setValue(index, forKey: element.documentURL?.absoluteString) 
             index = index + 1
        }  
    
    
        NSUserDefaults.standardUserDefaults().setObject(documentDic, forKey: "ReorderingFormat")
    }
    
  4. 一旦用户重新排序,就会更改对应于每个 url 的字典中的值。

    //Read data from User Defaults
    let dic = NSUserDefaults.standardUserDefaults().objectForKey("Reordering Format")
    for element in documents
    {
        //This way you get the order or index number for any directory.
        dic.objectForKey(element)
    }
    
  5. 当用户再次进入应用程序时,根据存储在 nsuserdefaults 中的字典值管理您的数组。