// // YMListViewController.swift // MvpGoods // // Created by 小花 on 2019/4/17. // Copyright © 2019 MVP. All rights reserved. // import UIKit import ObjectMapper import MJRefresh class YMListViewController: YMBaseViewController { var KGoodsCell = "KGoodsCell" var page = 1 var id: Int! var name: String? var collectionView: UICollectionView? var dataArr = [YMMainGoodModel]() var requestType: Int? override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.white self.title = name self.view.makeLoading(offY: navBarHeight) configCollectionView() loadGoodsData() } func loadGoodsData() { //为1时加载大分类下数据 if self.requestType == 1 { let url = BASE_URL + "/api/v2/goods/stockTopCategoryList" let para: [String: Any] = ["page":self.page, "category_id":self.id ?? 0, "sort":1, "is_has_coupon":1, "stype":"0"] NetWorkTool.postRequest(url: url, param: para, success: { (json) in let items = json["rst"]?["data"]["list"].arrayObject for item in items ?? [] { let model = Mapper().map(JSON: item as! [String : Any]) self.dataArr.append(model ?? YMMainGoodModel()) } self.view.dismissLoading() self.view.addSubview(self.collectionView!) self.collectionView?.reloadData() self.collectionView?.mj_footer.endRefreshing() }) { (error) in } }else { let url = BASE_URL + "/api/v2/goods/stocklistByCategoryId" let para: [String: Any] = ["page":self.page, "category_id":self.id, "sort":1, "is_has_coupon":1, "stype":"0"] NetWorkTool.postRequest(url: url, param: para, success: { (json) in let items = json["rst"]?["data"].arrayObject for item in items ?? [] { let model = Mapper().map(JSON: item as! [String : Any]) self.dataArr.append(model ?? YMMainGoodModel()) } self.view.addSubview(self.collectionView!) self.view.dismissLoading() self.collectionView?.reloadData() self.collectionView?.mj_footer.endRefreshing() }) { (error) in self.view.dismissLoading() } } } func configCollectionView() { let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: SCREENW, height: SCREENH-navHeight), collectionViewLayout:UICollectionViewFlowLayout()) collectionView.delegate = self collectionView.dataSource = self collectionView.backgroundColor = UIColor.gloadGrayColor() collectionView.showsVerticalScrollIndicator = false collectionView.register(YMMainGoodCollectionCell.self, forCellWithReuseIdentifier: KGoodsCell) self.collectionView = collectionView let footer = MJRefreshAutoNormalFooter() footer.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData)) collectionView.mj_footer = footer } @objc func loadMoreData() { self.page+=1 loadGoodsData() } } extension YMListViewController: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.dataArr.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KGoodsCell, for: indexPath) as! YMMainGoodCollectionCell cell.model = self.dataArr[indexPath.row] return cell } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: SCREEN_WIDTH, height: fitSize(x: 135)) } //最小 item 间距 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { return 0; } //最小行间距 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 5; } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let detail = YMDetailViewController() detail.goodModel = self.dataArr[indexPath.row] self.navigationController?.pushViewController(detail, animated: true) } }