BSSMainViewController.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // BSSMainViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2016/11/3.
  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 CocoaLumberjack
  14. private let reuseHeaderIdentifier = "BBSHeaderCollectionReusableView"
  15. private let reuseCellIdentifier = "BBSForumCell"
  16. class BSSMainViewController: UIViewController {
  17. @IBOutlet weak var collectionView: UICollectionView!
  18. //分区及板块数据集
  19. var bbsForums:[BBSForumListData] = []
  20. fileprivate static var ItemLineCount:Int {
  21. if UIDevice.deviceModelReadable() == "iPhone 5S" || UIDevice.deviceModelReadable() == "iPhone 5" || UIDevice.deviceModelReadable() == "iPhone SE" {
  22. return 3
  23. }else{
  24. return 4
  25. }
  26. }
  27. fileprivate let ItemSize = Double(SCREEN_WIDTH)/Double(ItemLineCount)
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. self.collectionView.dataSource = self
  31. self.collectionView.delegate = self
  32. self.loadForumDataAndSectionData()
  33. }
  34. func loadForumDataAndSectionData(){
  35. self.bbsForums.removeAll()
  36. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(BBSContext.bbsContextKey, query: BBSContext.getCategoryAndSectionQuery, parameter: nil)
  37. AF.request(url!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
  38. switch response.result {
  39. case .success(let val):
  40. let type = JSON(val)["type"]
  41. if type == "success" {
  42. let forumsJSON:[BBSForumListData] = Mapper<BBSForumListData>().mapArray(JSONString: JSON(val)["data"].description)!
  43. self.bbsForums.append(contentsOf: forumsJSON)
  44. }else{
  45. DDLogError(JSON(val).description)
  46. }
  47. case .failure(let err):
  48. DDLogError(err.localizedDescription)
  49. }
  50. self.collectionView.reloadData()
  51. }
  52. }
  53. @IBAction func backToSuper(_ sender: UIBarButtonItem) {
  54. let backType = AppConfigSettings.shared.appBackType
  55. if backType == 1 {
  56. self.performSegue(withIdentifier: "backToMain", sender: nil)
  57. }else if backType == 2 {
  58. self.performSegue(withIdentifier: "backToApps", sender: nil)
  59. }
  60. }
  61. override func didReceiveMemoryWarning() {
  62. super.didReceiveMemoryWarning()
  63. }
  64. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  65. if segue.identifier == "showSectionSubjectSegue" {
  66. let destVC = segue.destination as! BBSSubjectListViewController
  67. destVC.sectionData = sender as? BBSectionListData
  68. }
  69. }
  70. }
  71. extension BSSMainViewController:UICollectionViewDataSource{
  72. func numberOfSections(in collectionView: UICollectionView) -> Int {
  73. return bbsForums.count
  74. }
  75. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  76. return bbsForums[section].sectionInfoList?.count ?? 0
  77. }
  78. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  79. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseCellIdentifier, for: indexPath) as! BBSForumCell
  80. let sectionData = bbsForums[(indexPath as NSIndexPath).section].sectionInfoList![(indexPath as NSIndexPath).row]
  81. cell.bbsSectionData = sectionData
  82. return cell
  83. }
  84. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  85. if kind == UICollectionView.elementKindSectionHeader {
  86. let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: reuseHeaderIdentifier, for: indexPath) as! BBSHeaderCollectionReusableView
  87. let forumData = bbsForums[(indexPath as NSIndexPath).section]
  88. headerView.bbsForumData = forumData
  89. // headerView.backgroundColor = UIColor.white
  90. headerView.alpha = 0.7
  91. return headerView
  92. }else{
  93. return UICollectionReusableView(frame: CGRect(x: 0,y: 0,width: SCREEN_WIDTH,height: 1))
  94. }
  95. }
  96. }
  97. extension BSSMainViewController:UICollectionViewDelegateFlowLayout{
  98. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  99. let width = (SCREEN_WIDTH - 45) / 2
  100. return CGSize(width: CGFloat(width),height: CGFloat(205))
  101. }
  102. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  103. return UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)
  104. }
  105. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  106. return CGSize(width: SCREEN_WIDTH,height: 40)
  107. }
  108. func collectionView(_ collectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,referenceSizeForFooterInSection section: Int) -> CGSize {
  109. return CGSize(width: 0,height: 0)
  110. }
  111. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  112. return 10.0
  113. }
  114. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  115. return 0.0
  116. }
  117. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  118. DDLogDebug("cell clicked")
  119. let sectionData = bbsForums[(indexPath as NSIndexPath).section].sectionInfoList![(indexPath as NSIndexPath).row]
  120. self.performSegue(withIdentifier: "showSectionSubjectSegue", sender: sectionData)
  121. }
  122. }