FileMyDownloadViewController.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // FileMyDownloadViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 16/9/20.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import QuickLook
  10. import EmptyDataSet_Swift
  11. import CocoaLumberjack
  12. class FileMyDownloadViewController: UIViewController {
  13. @IBOutlet weak var tableView: ZLBaseTableView!
  14. var files:[String] = []
  15. var fileURLs:[URL] = []
  16. let quickLookController = QLPreviewController()
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. self.tableView.dataSource = self
  20. self.tableView.delegate = self
  21. self.tableView.emptyTitle = "您没有下载文件"
  22. self.tableView.emptyDesc = "您没有下载文件到本地,可以从前面的列表中选择文件并下载"
  23. self.loadMyFileFromDirectory()
  24. quickLookController.delegate = self
  25. quickLookController.dataSource = self
  26. }
  27. func loadMyFileFromDirectory(){
  28. let directoryURL = FileManager.default.urls(for: .downloadsDirectory,
  29. in: .userDomainMask)[0]
  30. let folder = directoryURL.appendingPathComponent("file", isDirectory: true)
  31. do {
  32. let subs = try FileManager.default.contentsOfDirectory(at: folder, includingPropertiesForKeys: nil, options: FileManager.DirectoryEnumerationOptions.skipsSubdirectoryDescendants)
  33. fileURLs.append(contentsOf: subs)
  34. getFileNameFromURLs()
  35. } catch {
  36. DDLogError("文件列表错误")
  37. }
  38. }
  39. func getFileNameFromURLs(){
  40. for fileURL in fileURLs {
  41. let fileName = fileURL.lastPathComponent
  42. files.append(fileName)
  43. }
  44. self.tableView.reloadData()
  45. }
  46. override func didReceiveMemoryWarning() {
  47. super.didReceiveMemoryWarning()
  48. // Dispose of any resources that can be recreated.
  49. }
  50. @IBAction func tableviewEditAction(_ sender: UIBarButtonItem) {
  51. tableView.setEditing(!tableView.isEditing, animated: true)
  52. if tableView.isEditing {
  53. sender.title = "取消"
  54. }else{
  55. sender.title = "编辑"
  56. }
  57. }
  58. //DataSource titleForEmptyDataSet
  59. }
  60. //extension FileMyDownloadViewController:DZNEmptyDataSetSource,DZNEmptyDataSetDelegate{
  61. //
  62. // func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
  63. //
  64. // let text = "没有下载的文件"
  65. // let attributes = [NSFontAttributeName:UIFont(name: "PingFangSC-Regular", size: 20.0)!,NSForegroundColorAttributeName:RGB(108, g: 108, b: 108)]
  66. // return NSAttributedString(string: text, attributes: attributes)
  67. // }
  68. //
  69. // func backgroundColor(forEmptyDataSet scrollView: UIScrollView!) -> UIColor! {
  70. // return RGB(247, g: 247, b: 247)
  71. // }
  72. //
  73. // func emptyDataSetShouldDisplay(_ scrollView: UIScrollView!) -> Bool {
  74. // return true
  75. // }
  76. //
  77. //}
  78. extension FileMyDownloadViewController:UITableViewDelegate,UITableViewDataSource{
  79. //是否可以编辑
  80. func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  81. return true
  82. }
  83. //设定编辑样式
  84. func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
  85. return .delete
  86. }
  87. //修改编辑文字
  88. func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
  89. return "删除"
  90. }
  91. //编辑提交
  92. func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
  93. switch editingStyle {
  94. case .none:
  95. break
  96. case .delete:
  97. //删除数据源
  98. let fileURL = self.fileURLs[(indexPath as NSIndexPath).row]
  99. do {
  100. try FileManager.default.removeItem(at: fileURL)
  101. } catch let error as NSError {
  102. DDLogError(error.debugDescription)
  103. }
  104. self.files.remove(at: (indexPath as NSIndexPath).row)
  105. //删除对应的行
  106. tableView.deleteRows(at: [indexPath], with: .left)
  107. case .insert:
  108. break
  109. }
  110. }
  111. func numberOfSections(in tableView: UITableView) -> Int {
  112. return 1
  113. }
  114. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  115. return self.files.count
  116. }
  117. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  118. let cell = tableView.dequeueReusableCell(withIdentifier: "MyFileItemCell", for: indexPath) as! MyFileItemCell
  119. let fileName = self.files[(indexPath as NSIndexPath).row]
  120. cell.fileName = fileName
  121. return cell
  122. }
  123. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  124. let fileURL = self.fileURLs[(indexPath as NSIndexPath).row]
  125. if QLPreviewController.canPreview(fileURL as QLPreviewItem){
  126. quickLookController.currentPreviewItemIndex = (indexPath as NSIndexPath).row
  127. navigationController?.pushViewController(quickLookController, animated: true)
  128. }
  129. }
  130. }
  131. extension FileMyDownloadViewController:QLPreviewControllerDelegate,QLPreviewControllerDataSource{
  132. func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
  133. return fileURLs.count
  134. }
  135. func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
  136. return fileURLs[index] as QLPreviewItem
  137. }
  138. }