我实现了一个表视图,它显示了一个文档列表
我使用此代码实现了重新排序功能
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: $0) }
tableView.reloadData()
}
它在重新排序时完全正常,但问题是当我关闭应用程序并再次启动它时(任何时候刷新数据函数运行),行返回到它们的默认位置。虽然我需要保存重新排序的更改。
答案 0 :(得分:1)
tableView中的重新排序仅适用于tableView。此重新排序不会影响原始数据源。关闭应用程序并再次运行后,重新加载数据的方法将从文档目录中获取原始数据。当tableView中此顺序发生变化时,您需要更改此原始文件的顺序。
从文档目录中获取数据。
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: $0) }
}
创建一个字典,其中key为文档目录文件或文件夹名称,值为索引。
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: $0)
let index = 0
let documentDic = NSMutableDictionary()
for element in documents
{
documentDic.setValue(index, forKey: element.documentURL?.absoluteString)
index = index + 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: $0)
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")
}
用户重新排序后,更改与每个网址对应的字典中的值。
//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)
}
当用户再次进入应用程序时,根据存储在nsuserdefaults中的字典值管理您的数组。