// // OrderController.swift // learnSwift // // Created by zyc on 2019/11/20. // Copyright © 2019 张言超. All rights reserved. // import UIKit class OrderController: BaseViewController { var myTableView = UITableView.init() /** 数据源 */ var dataArr: NSMutableArray = NSMutableArray.init() /** 选择的数据 */ var selectedArr: NSMutableArray = NSMutableArray.init() override func viewDidLoad() { super.viewDidLoad() initNavLeftBackButton() self.navigationItem.title = "购物车" self.customUI() self.initData() } var footView: YCOrderFootView? func customUI() { view.backgroundColor = UIColor.white self.myTableView = UITableView.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight-70), style: .grouped) self.myTableView.delegate = self self.myTableView.dataSource = self self.myTableView.separatorStyle = .none self.myTableView.register(YCOrderShopCartCell.self, forCellReuseIdentifier: "YCOrderShopCartCell") self.view.addSubview(self.myTableView) self.footView = YCOrderFootView.init(frame: CGRect(x: 0, y: kScreenHeight-navheight-70, width: kScreenWidth, height: 70)) self.footView?.footDelegate = self self.footView?.accountBtn?.addTarget(self, action: #selector(accountBtnTouch), for: UIControl.Event.touchUpInside) self.view.addSubview(self.footView!) } @objc func accountBtnTouch() { self.addToShopCar() } @objc func delectGoods(_ sender: UIButton) { let alert = UIAlertController.init(title: "您确定要删除这个商品吗?", message: "", preferredStyle: .alert) let confirmaction = UIAlertAction.init(title: "确定", style: .default) { (_) in let shopmodel: YCOrderShopModel = self.dataArr[0] as! YCOrderShopModel let arr: [YCOrderGoodsModel] = shopmodel.goodsArr! if arr.count > 0 { let goodmodel: YCOrderGoodsModel = arr[sender.tag] //删除选中的数组 if goodmodel.goodsIsSelect { let xxtempArr: NSMutableArray = self.selectedArr[0] as! NSMutableArray if xxtempArr.contains(goodmodel) { xxtempArr.remove(goodmodel) } } //删除数据源 var tempArr: [YCOrderGoodsModel] = NSMutableArray.init(array: arr) as! [YCOrderGoodsModel] let xxIndex: Int = tempArr.firstIndex(of: goodmodel)! var str = "" let ycmodel = tempArr[xxIndex] str = ycmodel.Id! self.delect(str) tempArr.remove(at: xxIndex) shopmodel.goodsArr = tempArr } //更新数据 self.updateData() } let cancleAction = UIAlertAction.init(title: "取消", style: .cancel, handler: nil) alert.addAction(confirmaction) alert.addAction(cancleAction) self.present(alert, animated: true, completion: nil) } func delect(_ string: String) { let params = NSMutableDictionary() params.setValue(string, forKey: "DATA_IDS") let http = AFHTTPSessionManager() http.post(RequestURL.deleteProductOrder, 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 { SVProgressHUD.showSuccess(withStatus: "删除成功!") self.initData() } }) { (_, _) in } } func initData() { 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 { self.dataArr.removeAllObjects() self.selectedArr.removeAllObjects() let dataArr: NSArray = (json as! NSDictionary).object(forKey: "data") as! NSArray if dataArr.count>0 { let dict: NSDictionary = dataArr[0] as! NSDictionary let shopModel = YCOrderShopModel.init() shopModel.setValue(dataArr, forKey: "commoditys") shopModel.setValue(dict["shop_id"], forKey: "shopName") self.dataArr.add(shopModel) } self.myTableView.reloadData() //初始化选择的数组 for _ in 0.. Int { return self.dataArr.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { let shopModel: YCOrderShopModel = self.dataArr[section] as! YCOrderShopModel return shopModel.goodsArr!.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: YCOrderShopCartCell = tableView.dequeueReusableCell(withIdentifier: "YCOrderShopCartCell", for: indexPath) as! YCOrderShopCartCell cell.backgroundColor = UIColor.colorWithCustom(r: 242, g: 242, b: 242) let shopModel: YCOrderShopModel = self.dataArr[indexPath.section] as! YCOrderShopModel cell.xgoodModel = shopModel.goodsArr?[indexPath.row] cell.YCcellDelegate = self cell.delectBtn?.addTarget(self, action: #selector(delectGoods(_:)), for: UIControl.Event.touchUpInside) cell.delectBtn!.tag = indexPath.row return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 100.0 } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: false) } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { // let model:YCOrderShopModel = self.dataArr[section] as! YCOrderShopModel // return model.goodsArr!.count > 0 ? 50 : 0 return 0 } func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let model: YCOrderShopModel = self.dataArr[section] as! YCOrderShopModel let headerview = YCOrderHeaderView.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 50)) headerview.shopModel = model headerview.index = section headerview.YCHeaderDelegate = self // return model.goodsArr!.count > 0 ? headerview : nil return nil } // func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { // if editingStyle == .delete { // let alert = UIAlertController.init(title: "您确定要删除这个商品吗?", message: "", preferredStyle: .alert) // let confirmaction = UIAlertAction.init(title: "确定", style: .default) { (UIAlertAction) in // let shopmodel:YCOrderShopModel = self.dataArr[indexPath.section] as! YCOrderShopModel // let arr:[YCOrderGoodsModel] = shopmodel.goodsArr! // if arr.count > 0{ // let goodmodel:YCOrderGoodsModel = arr[indexPath.row] // //删除选中的数组 // if goodmodel.goodsIsSelect { // let xxtempArr:NSMutableArray = self.selectedArr[indexPath.section] as! NSMutableArray // if xxtempArr.contains(goodmodel) { // xxtempArr.remove(goodmodel) // } // } // //删除数据源 // var tempArr:[YCOrderGoodsModel] = NSMutableArray.init(array: arr) as! [YCOrderGoodsModel] // let xxIndex:Int = tempArr.firstIndex(of: goodmodel)! // tempArr.remove(at: xxIndex) // shopmodel.goodsArr = tempArr // } // //更新数据 // self.updateData() // } // let cancleAction = UIAlertAction.init(title: "取消", style: .cancel, handler: nil) // alert.addAction(confirmaction) // alert.addAction(cancleAction) // self.present(alert, animated: true, completion: nil) // } // } // func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? { // return "删除" // } } // MARK: - - YCOrderShopCartCellDelegate extension OrderController: YCOrderShopCartCellDelegate { func shopCellDidPress(goodModel: YCOrderGoodsModel, cell: YCOrderShopCartCell) { print("点击cell,跳转商品详情") } func addBtnDidPress(goodModel: YCOrderGoodsModel, cell: YCOrderShopCartCell) { self.disposeShopData(goodModel: goodModel, cell: cell, isAdd: true) } func minusBtnDidPress(goodModel: YCOrderGoodsModel, cell: YCOrderShopCartCell) { self.disposeShopData(goodModel: goodModel, cell: cell, isAdd: false) } func leftSelBtnDidPress(goodModel: YCOrderGoodsModel, cell: YCOrderShopCartCell, isSel: Bool) { var shopModelIndex: Int? var goodModelIndex: Int? for shop_Index in 0.. 0 { for good_index in 0.. 0 { for good_index in 0.. 0 { for jindex in 0.. String? { let data = try? JSONSerialization.data(withJSONObject: dic, options: []) var str = String(data: data!, encoding: String.Encoding.utf8) str = str!.replacingOccurrences(of: "\"", with: "'") print(str!) return str } }