// // StudyViewController.swift // xingchuangke // // Created by Virgil on 2019/3/22. // Copyright © 2019 Virgil. All rights reserved. // import UIKit class StudyViewController: BaseViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { @IBOutlet weak var collectionView: UICollectionView! @IBOutlet weak var viewMenu4: UIView! @IBOutlet weak var viewMenu3: UIView! @IBOutlet weak var viewMenu2: UIView! @IBOutlet weak var viewMenu1: UIView! @IBOutlet weak var viewTop: UIView! override func viewDidLoad() { super.viewDidLoad() self.title = "我的学习" initNavLeftBackButton() //添加header 无header去掉 需要创建 UICollectionReusableView collectionView.register(UINib(nibName: "StudyCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "StudyCollectionReusableView") collectionView.register(UINib(nibName: "StudyCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "StudyCollectionViewCell") collectionView.delegate = self collectionView.dataSource = self collectionView.showsVerticalScrollIndicator = false collectionView.mj_header = MJRefreshNormalHeader(refreshingBlock: {[weak self] in self!.currentPage = 1 self!.loadData() }) collectionView.mj_header?.lastUpdatedTimeKey = "NewsListViewController" collectionView.mj_footer = MJRefreshBackNormalFooter(refreshingBlock: {[weak self] in self!.currentPage += 1 self!.loadData() }) //设置header 大小 无header去掉 let collectionLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout collectionLayout.headerReferenceSize = CGSize(width: ScreenWidth, height: 44) collectionView.mj_header?.beginRefreshing() } // MARK: =============加载数据=============== func loadData() { let url = RequestURL.onlineList let params = NSMutableDictionary() params.setValue("1", forKey: "isRec") params.setValue("FFFFFF", forKey: "typeId") loadDataListForCollectionView(url: url, params: params, collectionView: collectionView, tag: 1001) } override func returnData(tag: Int) { if tag == 1001 { collectionView.reloadData() } } override func returnError(tag: Int, type: String) { } //btnAddTarget(view: self.view, selector: #selector(self.btnMenuClick)) @IBAction func btnMenuClick(_ sender: AnyObject) { switch (sender as! UIButton).tag { case 1001: // 在线学习 appDelegate.navController.pushViewController(StudyTypeViewController(), animated: true) break case 1002: // 线下培训 appDelegate.navController.pushViewController(UnderLineTrainViewController(), animated: true) break case 1003: // 我的课程 appDelegate.navController.pushViewController(MyClassViewController(), animated: true) break case 1004: // 知识库 appDelegate.navController.pushViewController(KnowledgeBaseViewController(), animated: true) break case 1005: // break case 1006: // break default: break } } //返回分区个数 func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } //返回每个分区的item个数 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.arrData.count } //定义每个UICollectionView 的大小 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: ScreenWidth / 2, height: (ScreenWidth - 30) / 2 / 170 * 103 + 68) } //初始化header func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { var reusableView: StudyCollectionReusableView! if kind == UICollectionView.elementKindSectionHeader { reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "StudyCollectionReusableView", for: indexPath) as! StudyCollectionReusableView if reusableView.subviews.count < 1 { //reusableView.addSubview(viewTop); } } else { reusableView = StudyCollectionReusableView() } return reusableView } //初始化cell func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "StudyCollectionViewCell", for: indexPath) as! StudyCollectionViewCell cell.initCell(rowIndex: indexPath.row, dic: self.arrData[indexPath.row] as! NSDictionary) return cell } //定义每个UICollectionView 的间距 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) } //定义每个UICollectionView 纵向的间距 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { return 0 } //选中 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let vc = StudyDetailViewController() vc.dicData.setDic(dic: self.arrData[indexPath.row] as! NSDictionary) appDelegate.navController.pushViewController(vc, animated: true) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) appDelegate.setNavigationBarHidden(isHidden: false) self.tabBarController?.title = "我的学习" } override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { super.init(nibName: "StudyViewController", bundle: nil) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }