// // ShoppingMallViewController.swift // CommonFrame // // Created by Virgil on 2019/4/18. // Copyright © 2019 Virgil. All rights reserved. // import UIKit class ShoppingMallCollectionViewController: BaseViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { @IBOutlet weak var collectionView: UICollectionView! let numLabel = UILabel.init() override func viewDidLoad() { super.viewDidLoad() self.navInit() self.loadType() collectionView.register(UINib(nibName: "StudyCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "StudyCollectionReusableView") collectionView.register(UINib(nibName: "ShoppingMallCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ShoppingMallCollectionViewCell") 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: 16) collectionView.mj_header?.beginRefreshing() let shopBtn = UIButton.init() shopBtn.frame = CGRect(x: kScreenWidth-60, y: kScreenHeight-150, width: 45, height: 45) shopBtn.addTarget(self, action: #selector(goShopCart), for: .touchUpInside) shopBtn.setImage(UIImage.init(named: "icon_shop_cart"), for: .normal) self.view.addSubview(shopBtn) numLabel.frame = CGRect(x: shopBtn.mj_x+35, y: shopBtn.mj_y+8, width: 15, height: 15) // numLabel.center = shopCarBtn.center numLabel.backgroundColor = .red numLabel.font = UIFont.systemFont(ofSize: 10) numLabel.textColor = .white numLabel.layer.cornerRadius = 7.5 numLabel.textAlignment = .center numLabel.layer.masksToBounds = true numLabel.isHidden = true self.view.addSubview(numLabel) loadNum() } // 自定义导航栏 func navInit() { let viewNav = (CommonViewUntils.getViewForXIB(xibName: "NavView") as! NavView) viewNav.initView(title: "商城") {[weak self] (index, _) in if index == 1 { // self!.handleBack(); // 我的订单 let vc = MyOrderViewController() self?.navigationController?.pushViewController(vc, animated: true) } } self.view.addSubview(viewNav) viewNav.btnLeft.isHidden = true viewNav.initRightBtn(title: "我的订单") } @objc func goShopCart() { toViewController(viewController: OrderController()) } var typeId = "" ///加载头部的菜单 func loadMenu() { var arrTitle = [String]() for i in 0 ..< arrType.count { arrTitle.append(arrType.getString(index: i, key: "type_name")) } let segment = Virgil_SegmentView(frame: CGRect(x: 0, y: common_nav_status_height, width: ScreenWidth, height: 47), 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!.currentPage = 1 let dic = self?.arrType[index] as!NSDictionary self?.typeId = "\(dic["product_type"] as! Int)" self!.collectionView.mj_header?.beginRefreshing() } self.view.addSubview(segment) let dic = self.arrType[0] as!NSDictionary self.typeId = "\(dic["product_type"] as! Int)" collectionView.mj_header?.beginRefreshing() } // MARK: =============加载数据=============== var productType = 1 func loadData() { // let url = RequestURL.productPageList; let params = NSMutableDictionary() params.setValue(CommonValue.getUserId(), forKey: "userId") params.setValue(self.typeId, forKey: "productType") TloadDataListForCollectionView(url: RequestURL.getProductList, params: params, collectionView: collectionView, tag: 1001) } var arrType = NSArray() // MARK: =============加载数据=============== func loadType() { let params = NSMutableDictionary() params.setValue(CommonValue.getUserId(), forKey: "userId") // loadDataList(url: url, params: params, tag: 1001, array: arrType) let http = AFHTTPSessionManager() http.get(RequestURL.getProductType, parameters: params, progress: { (_) in }, success: { (operation, json) in print(json as Any) let success = (json as! NSDictionary).object(forKey: "statusCode") as! Int if success == 200 { self.arrType = (json as! NSDictionary).object(forKey: "data") as! NSArray self.loadMenu() } }) { (_, _) in } } override func returnData(tag: Int) { if tag == 1001 { 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 + 58) } //初始化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 reusableView.lblTitle.isHidden = true reusableView.lblLine.isHidden = true } else { reusableView = StudyCollectionReusableView() } return reusableView } //初始化cell func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ShoppingMallCollectionViewCell", for: indexPath) as! ShoppingMallCollectionViewCell 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 view = ShoppingMallDetailViewController() view.id = getString(current: indexPath.row, key: "id") self.navigationController?.pushViewController(view, animated: true) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) appDelegate.setNavigationBarHidden(isHidden: true) loadNum() } override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { super.init(nibName: "ShoppingMallCollectionViewController", bundle: nil) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func loadNum() { let params = NSMutableDictionary() params.setValue(CommonValue.getUserId(), forKey: "userId") let http = AFHTTPSessionManager() http.get(RequestURL.getProductOrderList, parameters: params, progress: { (_) in }, success: { (operation, json) in print(json as Any) let success = (json as! NSDictionary).object(forKey: "statusCode") as! Int if success == 200 { let dataArr: NSArray = (json as! NSDictionary).object(forKey: "data") as! NSArray if dataArr.count > 0 { self.numLabel.isHidden = false self.numLabel.text = "\(dataArr.count)" } else { self.numLabel.isHidden = true } } }) { (_, _) in } } }