【发布时间】:2017-10-03 10:42:49
【问题描述】:
我实现了一个表格视图,它显示了一个文档列表
我用这段代码实现了重新排序功能
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()
}
它在重新排序时完全正常,但问题是当我关闭应用程序并重新启动它时(任何时候刷新数据功能运行),行都会回到它们的默认位置。而我需要在重新排序时保存更改。
【问题讨论】:
标签: ios swift uitableview file-manager