FileFolderSelectViewController.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // FileFolderSelectViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 16/9/19.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Alamofire
  10. import AlamofireImage
  11. import AlamofireObjectMapper
  12. import ObjectMapper
  13. import SwiftyJSON
  14. import CocoaLumberjack
  15. protocol FileFolderPassValueDelegate {
  16. func selectedFolder(_ f:OOFile)
  17. }
  18. class FileFolderSelectViewController: UIViewController {
  19. @IBOutlet weak var tableView: ZLBaseTableView!
  20. var folders:[OOFile] = []
  21. var sFolder:OOFile?
  22. //文件夹层次队列
  23. var folderQueue:[OOFile] = []
  24. var delegate:FileFolderPassValueDelegate?
  25. override func viewDidLoad() {
  26. super.viewDidLoad()
  27. self.tableView.delegate = self
  28. self.tableView.dataSource = self
  29. self.tableView.emptyTitle = "没有可供选择的文件夹"
  30. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileTopListQuery, parameter: nil)!
  31. self.loadDataRequestFileCompleted(url)
  32. }
  33. override func didReceiveMemoryWarning() {
  34. super.didReceiveMemoryWarning()
  35. }
  36. @IBAction func btnSure(_ sender: UIBarButtonItem) {
  37. self.dismiss(animated: true) {
  38. if self.sFolder != nil {
  39. self.delegate?.selectedFolder(self.sFolder!)
  40. }
  41. }
  42. }
  43. @IBAction func btnCancel(_ sender: UIBarButtonItem) {
  44. self.dismiss(animated: true, completion: nil)
  45. }
  46. }
  47. extension FileFolderSelectViewController:FileFolderCellPassValueDelegate{
  48. //cellDelegate
  49. func selectedCellPassValue(_ cell: FileFolderItemCell, f: OOFile) {
  50. self.sFolder = f
  51. self.tableView.beginUpdates()
  52. let cells = self.tableView.visibleCells
  53. for c in cells {
  54. let itemCell = c as! FileFolderItemCell
  55. if itemCell != cell {
  56. itemCell.actionButton.isSelected = false
  57. }
  58. }
  59. self.tableView.endUpdates()
  60. }
  61. }
  62. extension FileFolderSelectViewController:FolderHeaderViewDelegate{
  63. func headerClickSelected(currentFile f: OOFile, folderQueue fQueue: [OOFile]) {
  64. self.folderQueue = fQueue
  65. var url = ""
  66. if f.id == "0" {
  67. url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileTopListQuery, parameter: nil)!
  68. }else{
  69. url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileFolderItemIdQuery, parameter: ["##id##":f.id! as AnyObject])!
  70. }
  71. self.loadDataRequestFileCompleted(url)
  72. }
  73. }
  74. extension FileFolderSelectViewController:UITableViewDelegate,UITableViewDataSource{
  75. func numberOfSections(in tableView: UITableView) -> Int {
  76. return 1
  77. }
  78. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  79. return self.folders.count
  80. }
  81. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  82. return 40.0
  83. }
  84. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  85. let headerView = FolderHeaderView()
  86. headerView.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 40)
  87. headerView.folderQueue = self.folderQueue
  88. headerView.delegate = self
  89. return headerView
  90. }
  91. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  92. let cell = tableView.dequeueReusableCell(withIdentifier: "FileFolderItemCell", for: indexPath) as! FileFolderItemCell
  93. let file = self.folders[(indexPath as NSIndexPath).row]
  94. cell.file = file
  95. cell.delegate = self
  96. return cell
  97. }
  98. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  99. let f = self.folders[(indexPath as NSIndexPath).row]
  100. self.folderQueue.append(f)
  101. //读取
  102. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileFolderItemIdQuery, parameter: ["##id##":f.id! as AnyObject])
  103. self.loadDataRequestFileCompleted(url!)
  104. }
  105. func loadDataRequestFileCompleted(_ url:String){
  106. self.showLoading(title: "加载中")
  107. AF.request(url).responseJSON { response in
  108. self.folders.removeAll()
  109. switch response.result {
  110. case .success(let val):
  111. let json = JSON(val)["data"]
  112. let folders = Mapper<OOFile>().mapArray(JSONString:json["folderList"].description)
  113. self.folders.append(contentsOf: folders!)
  114. self.tableView.reloadData()
  115. self.showSuccess(title: "加载完成")
  116. case .failure(let err):
  117. DDLogError(err.localizedDescription)
  118. self.tableView.reloadData()
  119. self.showError(title: "加载失败")
  120. }
  121. }
  122. }
  123. }