CMSApplicationTableViewController.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // CMSApplicationTableViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2016/12/8.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Alamofire
  10. import AlamofireImage
  11. import AlamofireObjectMapper
  12. import SwiftyJSON
  13. import ObjectMapper
  14. import CocoaLumberjack
  15. class CMSApplicationTableViewController: UITableViewController {
  16. var cmsApplication:CMSApplication?
  17. var pageModel:SubjectPageModel = SubjectPageModel()
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20. tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
  21. self.pageModel = SubjectPageModel()
  22. self.loadFirstData()
  23. })
  24. self.loadFirstData()
  25. }
  26. private func loadFirstData(){
  27. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(CMSContext.cmsContextKey, query: CMSContext.cmsCategoryQuery, parameter: nil)
  28. self.cmsApplication = nil
  29. AF.request(url!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
  30. switch response.result {
  31. case .success(let val):
  32. self.cmsApplication = Mapper<CMSApplication>().map(JSONObject: val)
  33. self.pageModel.setPageTotal((self.cmsApplication?.count!)!)
  34. case .failure(let err):
  35. DDLogError(err.localizedDescription)
  36. }
  37. DispatchQueue.main.async {
  38. self.tableView.reloadData()
  39. if self.tableView.mj_header.isRefreshing(){
  40. self.tableView.mj_header.endRefreshing()
  41. }
  42. }
  43. }
  44. }
  45. @IBAction func backToSuper(_ sender: UIBarButtonItem) {
  46. let backType = AppConfigSettings.shared.appBackType
  47. if backType == 1 {
  48. self.performSegue(withIdentifier: "backToMain", sender: nil)
  49. }else if backType == 2 {
  50. self.performSegue(withIdentifier: "backToApps", sender: nil)
  51. }
  52. }
  53. override func didReceiveMemoryWarning() {
  54. super.didReceiveMemoryWarning()
  55. // Dispose of any resources that can be recreated.
  56. }
  57. // MARK: - Table view data source
  58. override func numberOfSections(in tableView: UITableView) -> Int {
  59. // #warning Incomplete implementation, return the number of sections
  60. return 1
  61. }
  62. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  63. guard let application = cmsApplication,(cmsApplication?.data?.count)! > 0 else {
  64. return 0
  65. }
  66. return (application.data?.count)!
  67. }
  68. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  69. let cell = tableView.dequeueReusableCell(withIdentifier: "CMSCategoryItemCell", for: indexPath) as! CMSCategoryItemCell
  70. let cmsData = self.cmsApplication?.data?[indexPath.row]
  71. cell.cmsData = cmsData
  72. return cell
  73. }
  74. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  75. let cmsData = self.cmsApplication?.data?[indexPath.row]
  76. if cmsData?.wrapOutCategoryList != nil {
  77. self.performSegue(withIdentifier: "showCategorySegue", sender: cmsData)
  78. }else {
  79. self.showError(title: "该栏目为空,没有数据!")
  80. }
  81. }
  82. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  83. if segue.identifier == "showCategorySegue" {
  84. let cmsData = sender as? CMSData
  85. let destVC = segue.destination as! CMSCategoryListViewController
  86. destVC.title = cmsData?.appName
  87. destVC.cmsData = cmsData
  88. }
  89. }
  90. }