// // DZGLViewController.swift // GeliBusinessPlatform // // Created by junye lu on 2020/5/7. // Copyright © 2020 junye lu. All rights reserved. // import UIKit import SwipeCellKit import LGButton protocol DZGLViewControllerDelegate { func DZGLViewControllerSelect(data:GetAddrsInfoDataModel) } class DZGLViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource,SwipeTableViewCellDelegate,UISearchBarDelegate,SearchBarViewDelegate,XinZengDiZhiViewControllerDelegate,GeliAlertViewDelegate{ func sureGeliAlertViewAction(sender: UIButton) { let UserToken = UserDefaults.standard.value(forKey: "user_token") let model = adrArrs[sender.tag] AddressDel(["user_token":UserToken as Any,"address_id":model.address_id as Any], success: { (data) in self.listView.mj_header?.beginRefreshing() }) { (error) in } } func XinZengDiZhiViewControllerFinishAction() { listView.mj_header?.beginRefreshing() } //MARK:--搜索模块 //MAKR:--调起搜索后添加个蒙版本 var blackBtnView:UIButton! = nil func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool { print("调起搜索后添加个蒙版本") blackBtnView = UIButton () blackBtnView.backgroundColor = UIColor.init(named: "提示视图背景色") view.addSubview(blackBtnView) blackBtnView.snp.makeConstraints { (make) in make.left.right.bottom.equalToSuperview() make.top.equalTo(searchBg.snp_bottom) } blackBtnView.addTarget(self, action: #selector(removeBlackBtnView), for: .touchUpInside) return true } @objc func removeBlackBtnView(){ if blackBtnView != nil { blackBtnView.removeFromSuperview() searchV!.resignFirstResponder() } } //MARK:--搜索按钮代理 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { print("点击搜索关键字进行--\(searchBar.text)") searchBar.resignFirstResponder() if blackBtnView != nil { blackBtnView.removeFromSuperview() } keyWord = searchBar.text! listView.mj_header?.beginRefreshing() } //MARK:--清空搜索代理 func clearSearchAction() { } //MARK:--编辑即将结束 func searchBarTextDidEndEditing(_ searchBar: UISearchBar) { print("编辑即将结束了--") removeBlackBtnView() if keyWord.count > 0 { keyWord = searchBar.text! if keyWord.count == 0 { listView.mj_header?.beginRefreshing() } } } var delegate:DZGLViewControllerDelegate? var cus_id:Int? var isSelectMotion = false var selectIdx:IndexPath? var adrInfo:GetAddrsInfoModel? //列表 var adrArrs:Array = [] @IBOutlet weak var searchBg: UIView! @IBOutlet weak var btmLbl: UILabel! @IBOutlet weak var listView: UITableView! @IBOutlet weak var searchBgH: NSLayoutConstraint! var searchV : SearchBarView? = nil @IBOutlet weak var btmBtn: LGButton! @IBAction func btmClick(_ sender: Any) { delegate?.DZGLViewControllerSelect(data: (adrInfo?.data![selectIdx!.row])!) self.navigationController?.popViewController(animated: true) } @IBOutlet weak var btmLblHeight: NSLayoutConstraint! var keyWord = "" override func viewDidLoad() { super.viewDidLoad() navbar.title = "地址管理" self.view.addSubview(navbar) let UserToken = UserDefaults.standard.value(forKey: "user_token") if cus_id != nil { HUD.flash(.progress) searchBgH.constant = 0 getAddrsInfo(["user_token":UserToken as Any,"cus_id":cus_id as Any], success: { (data) in self.adrInfo = (data as! GetAddrsInfoModel) self.listView.reloadData() HUD.hide() }) { (error) in } }else{ //searchbar let searchBr = SearchBarView.init() self.searchBg.addSubview(searchBr) searchBr.snp.makeConstraints { (make) in make.left.right.equalTo(0) make.centerY.equalToSuperview() make.height.equalTo(35) } searchV = searchBr searchBr.placeholderStr = "请输入收货人地址" searchBr.cornerRadius = 35/2.0 searchBr.delegate = self searchBr.delegateL = self navbar.rightTitle = "新增" searchBgH.constant = 45 //下拉刷新相关设置 headerMJ.setRefreshingTarget(self, refreshingAction: #selector(loadData)) self.listView!.mj_header = headerMJ self.listView.mj_header?.beginRefreshing() } listView.separatorStyle = .none // SetTopFrameView(view: listView, btmView: btmLbl) SetTopFrame(view: searchBg, height: 45) listView.register(UINib(nibName: "DiZhiGuanLiCell", bundle: nil), forCellReuseIdentifier: "DiZhiGuanLiCell") btmLbl.isHidden = !isSelectMotion btmBtn.isHidden = !isSelectMotion if isSelectMotion { btmLblHeight.constant = 49 }else{ btmLblHeight.constant = 0 } listView.estimatedRowHeight = 49 listView.rowHeight = UITableView.automaticDimension // Do any additional setup after loading the view. } @objc func loadData(){ let UserToken = UserDefaults.standard.value(forKey: "user_token") adrArrs.removeAll() AddressList(["user_token":UserToken as Any,"key_word":keyWord], success: { (data) in let model = data as! AddressListModel model.data?.forEach({ (item) in self.adrArrs.append(item) }) self.listView.reloadData() self.listView.mj_header?.endRefreshing() }) { (data) in self.listView.mj_header?.endRefreshing() } } func rightBtnClick() { let vc = XinZengDiZhiViewController() vc.navTitle = "新增地址" vc.delegate = self self.navigationController?.pushViewController(vc, animated: true) } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if cus_id != nil { if adrInfo != nil { return (adrInfo?.data!.count)! } } return adrArrs.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "DiZhiGuanLiCell") as! DiZhiGuanLiCell cell.delegate = self if cus_id != nil {//选择 let model = adrInfo?.data![indexPath.row] cell.nameLbl.text = model?.consignee cell.phoneLbl.text = model?.mobile cell.adrLbl.text = model?.addr_cn if isSelectMotion == false { cell.btnH.constant = 0 cell.xValue.constant = 0 }else{ if indexPath == selectIdx { cell.btn.isSelected = true } } }else{//列表 if adrArrs.count > 0 { let model = adrArrs[indexPath.row] cell.nameLbl.text = model.consignee cell.phoneLbl.text = model.mobile cell.adrLbl.text = model.province_cn! + model.address! if model.city_cn != nil { cell.adrLbl.text = model.province_cn! + model.city_cn! + model.address! if model.district_cn != nil { cell.adrLbl.text = model.province_cn! + model.city_cn! + model.district_cn! + model.address! } } cell.btnH.constant = 0 cell.xValue.constant = 0 cell.blueLbl.isHidden = true cell.yellowLbl.isHidden = true if model.is_default == 1 { cell.yellowLbl.isHidden = false cell.blueLbl.isHidden = true cell.yellowLblLeft.constant = -18 } if model.is_send_default == 1 { cell.blueLbl.isHidden = false cell.yellowLbl.isHidden = true } if (model.is_send_default == 1) && (model.is_default == 1) { cell.yellowLbl.isHidden = false cell.blueLbl.isHidden = false cell.yellowLblLeft.constant = 10 } } } return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print(indexPath.row) if isSelectMotion == false { if adrArrs.count > 0 { let model = adrArrs[indexPath.row] let vc = XinZengDiZhiViewController() vc.delegate = self vc.navTitle = "编辑地址" vc.provinceIndex = model.province! vc.cityIndex = model.city! vc.areaIndex = model.district! vc.address_id = model.address_id! vc.adrStr = model.address! vc.nameStr = model.consignee! vc.phone = model.mobile! vc.tel = model.tel! vc.zip = model.zipcode! vc.is_send_default = false vc.pcdStr = model.province_cn! if model.city_cn != nil { vc.pcdStr = model.province_cn! + "-" + model.city_cn! if model.district_cn != nil { vc.pcdStr = model.province_cn! + "-" + model.city_cn! + "-" + model.district_cn! } } if model.is_send_default == 1 { vc.is_send_default = true } vc.is_default = false if model.is_default == 1 { vc.is_default = true } self.navigationController?.pushViewController(vc, animated: true) } }else{ selectIdx = indexPath listView.reloadData() } } //MARK:- 左滑删除 func tableView(_ tableView: UITableView, editActionsOptionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeTableOptions { var options = SwipeTableOptions() options.transitionStyle = .border //变化样式(使用默认的不变) options.expansionStyle = .selection //展开样式(默认为.none) return options } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return tableView.rowHeight } func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? { let tag = indexPath.row //创建“删除”事件按钮 let deleteAction = SwipeAction(style: .destructive, title: nil) { action, indexPath in //将对应条目的数据删除 let alertView = GeliAlertView(frame: self.view.window!.bounds) alertView.delegate = self alertView.tishiLbl.text = "" alertView.titileLbl.text = "是否删除地址?" alertView.rightBtn.setAttributedTitle(NSAttributedString(string: "确定"), for: .normal) alertView.rightBtn.tag = tag self.view.window?.addSubview(alertView) } deleteAction.image = UIImage(named: "shanchuWhite") deleteAction.backgroundColor = UIColor(named: "#F57575") //返回右侧事件按钮 return [deleteAction,] } }