// // StudyViewController.swift // xingchuangke // // Created by Virgil on 2019/3/22. // Copyright © 2019 Virgil. All rights reserved. // import UIKit class StudyTypeViewController: BaseViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { @IBOutlet weak var collectionView: UICollectionView! @IBOutlet weak var viewTop: UIView! var currentType = 0 override func viewDidLoad() { super.viewDidLoad() initNavLeftBackButton() self.title = "在线学习" 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() }) } // MARK: =============加载数据=============== let arrType = NSMutableArray() func loadType() { if arrType.count > 0 { returnData(tag: 1001) return } let url = RequestURL.studyTypeList let params = NSMutableDictionary() params.setValue(0, forKey: "type") loadDataList(url: url, params: params, tag: 1001, array: arrType) } func loadData() { let url = RequestURL.onlineList let params = NSMutableDictionary() params.setValue("0", forKey: "isRec") params.setValue(arrType.getString(index: currentType, key: "id"), forKey: "typeId") loadDataListForCollectionView(url: url, params: params, collectionView: collectionView, tag: 1002) } override func returnData(tag: Int) { if tag == 1001 { if arrType.count < 1 { SVProgressHUD.showError(withStatus: "学习分类获取出错,请联系客服!") return } var arrTitle = [String]() for dic in self.arrType { arrTitle.append((dic as! NSDictionary).getString(key: "name")) } let segment = Virgil_SegmentView(frame: CGRect(x: 0, y: 3, width: ScreenWidth, height: 45), titles: arrTitle, defaultColor: CommonUntils.getUIColorFromRGB(rgbValue: 0x333333, alpha: 1.0), currentColor: CommonUntils.getUIColorFromRGB(rgbValue: 0x2094f5, alpha: 1.0), lineHeight: 2, lineIsBottom: true) { [weak self](index) in self!.currentType = index self!.collectionView.mj_header?.beginRefreshing() } self.viewTop.removeAll() self.viewTop.addSubview(segment) self.collectionView.mj_header?.beginRefreshing() } else if tag == 1002 { collectionView.reloadData() } } override func returnError(tag: Int, type: String) { } //返回分区个数 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) } //初始化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: 20, 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) loadType() } override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { super.init(nibName: "StudyTypeViewController", bundle: nil) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }