iOS-swift学习笔记-2

import UIKit
import SVProgressHUD


class bourseViewController: UIViewController, UITableViewDataSource, UITableViewDelegate,StarsSortTableViewDelegate,StarsScreenViewDelegate {
 
    var cells = ["默认排序", "按价格排序", "按代数排序", "按生命排序", "按品质排序", "按等级排序", "按文明排序", "按探索排序"]
    /// 排序方式
    var sorts = ["","price","parent_id","life","quality","level","culture","ability"]
    
    
    var ordercells = ["默认排序", "顺序", "倒叙"]
    /// 排序方式
    var ordersorts = ["","ASC","DESC"]
    
    
    @IBOutlet  var segmentcontrol: UISegmentedControl!
    
    @IBOutlet  var leftableview: UITableView!
    
    @IBOutlet  var rightableview: UITableView!
    
    // 底部加载
    let footer = MJRefreshAutoNormalFooter()
    var items = [StarsHomeItem]()
    var type = Int()
    var order = String()
    var sortnum = String()
    var clicknum = 0
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.topViewController?.title = "交易所"
        self.view.backgroundColor = UIColor.white
        setupSegment()
        setupTableView()
        loadData()
        setupTitlesView()
        //上刷新相关设置
        footer.setRefreshingTarget(self, refreshingAction: #selector(bourseViewController.footerLoad))
        //是否自动加载(默认为true,即表格滑到底部就自动加载)
        footer.isAutomaticallyRefresh = false
        self.leftableview!.mj_footer = footer
        self.rightableview!.mj_footer = footer
    }
    
    //分段按钮
    func setupSegment()
    {
        let itemtitles=["购买星球","我的挂单"]
        segmentcontrol = UISegmentedControl.init(items: itemtitles)
        segmentcontrol.frame = CGRect(x: 46, y: 65, width: 322, height: 35)
        segmentcontrol.selectedSegmentIndex = 0 //默认选中第1项
        type = 0
        segmentcontrol.addTarget(self, action: #selector(bourseViewController.segmentDidchange(_:)),
                                 for: .valueChanged)  //添加值改变监听
        self.view.addSubview(segmentcontrol!)
    }
    
    @objc func segmentDidchange(_ segmented:UISegmentedControl){
        //获得选项的索引
       // print(segmented.selectedSegmentIndex)
        switch (segmented.selectedSegmentIndex)
        {
        case 0:
            leftableview.isHidden = false
            rightableview.isHidden = true
            type = 0
            break
        case 1:
            leftableview.isHidden = true
            rightableview.isHidden = false
            type = 1
            break
        default:
            break
        }
       
    }
    
    
    
    /// 顶部标签栏
     func setupTitlesView() {
        // 顶部背景
        let bgView = UIView()
        bgView.backgroundColor = StarsGlobalColor()
        bgView.frame = CGRect(x: 0, y: kTitlesViewY + 35, width: SCREENW, height: kTitlesViewH)
        view.addSubview(bgView)
        
        //排序
        let sortLabel = UILabel()
        sortLabel.text = "排序:"
        sortLabel.textAlignment = .center
        sortLabel.frame = CGRect(x: 20, y: 0, width: 42, height: kTitlesViewH)
        sortLabel.textColor = StarsColor(r: 20, g: 6, b: 16, a: 1.0)
        bgView.addSubview(sortLabel)
       
        // 选择按钮
         bgView.addSubview(arrowButton)
        arrowButton.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 102, height: kTitlesViewH))
            make.left.equalTo(55)
            make.top.equalTo(0)
        }
        
        //顺序
        let rankLabel = UILabel()
        rankLabel.text = "排序:"
        rankLabel.textAlignment = .center
        rankLabel.frame = CGRect(x: 180, y: 0, width: 42, height: kTitlesViewH)
        rankLabel.textColor = StarsColor(r: 20, g: 6, b: 16, a: 1.0)
        bgView.addSubview(rankLabel)
        
        // 选择按钮
        bgView.addSubview(orderButton)
        orderButton.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 102, height: kTitlesViewH))
            make.left.equalTo(210)
            make.top.equalTo(0)
        }
        
        //筛选按钮
        bgView.addSubview(screenButton)
        screenButton.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 95, height: kTitlesViewH))
            make.right.equalTo(0)
            make.top.equalTo(0)
        }
        
        
    }
    
    
    // MARK: - YMSortTableViewDelegate
    func sortView(sortView: bourseSortTableView, choicenum: Int) {
        /// 根据搜索条件进行搜索//排序字段(price,parent_id,life,quality,level,culture,ability)
       // "sort":sort,         //排序,正序为ASC倒序为DESC
        if clicknum == 1{
            order = sorts[choicenum]
            arrowButton.setTitle(cells[choicenum], for: .normal)
        }else{
            sortnum = ordersorts[choicenum]
            orderButton.setTitle(ordercells[choicenum], for: .normal)
        }
        
      print("order",order,sortnum)
        //加载数据
        weak var weakSelf = self
        NetWorkTool.shareNetworkTool.loadHomeInfo(status: "", cointype: "", order: order, sort: sortnum, type: "", page: boursepagenum) { (homeItems) in
            weakSelf!.items = homeItems
            print("dismiss")
            sortView.dismiss()
            weakSelf?.leftableview.reloadData()
        }
    }
    
    func screentView(screenView: bourseScreenView, Starstype: String, Statetype: String, Cointype: String) {
        //  "type":type,         //默认所有星球,初代星球=1,幻想=2
        //  "status":status,       //待交易=2,待博弈=3,勾选其他传空,默认不勾选传空
        //  "coin_type":cointype,    //1-eth,2-sts,默认不选传空
        
        //加载数据
        weak var weakSelf = self
        NetWorkTool.shareNetworkTool.loadHomeInfo(status: Statetype, cointype: Cointype, order: order, sort: sortnum, type: Starstype, page: boursepagenum) { (homeItems) in
            weakSelf!.items = homeItems
            print("screendismiss")
            screenView.dismiss()
            weakSelf?.leftableview.reloadData()
            
        }
    }
    
    /// 搜索条件点击
    @objc func sortButtonClick(button: UIButton) {

        UIView.animate(withDuration: 0.5) {
            button.imageView?.transform = button.imageView!.transform.rotated(by: CGFloat(M_PI))
        }
        clicknum = 1
        popView.show()
        orderView.dismiss()
        screenView.dismiss()
    }
    
    /// 搜索条件点击
    @objc func orderButtonClick(button: UIButton) {

        UIView.animate(withDuration: 0.5) {
            button.imageView?.transform = button.imageView!.transform.rotated(by: CGFloat(M_PI))
        }
        clicknum = 2
        orderView.show()
        popView.dismiss()
        screenView.dismiss()
    }
    
    @objc func screenButtonClick(button:UIButton){
        UIView.animate(withDuration: 0.5) {
            button.imageView?.transform = button.imageView!.transform.rotated(by: CGFloat(M_PI))
        }
        screenView.show()
        orderView.dismiss()
        popView.dismiss()
    }
    
    private lazy var popView: bourseSortTableView = {
        let popView = bourseSortTableView()
        for i in 0.. Int {
        if type == 0{
            
            return items.count
        }
        else if (type == 1){
            return items.count
        }
        return 0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //设置单元格数据
        if type == 0{
            
            let homeCell = tableView.dequeueReusableCell(withIdentifier: StarsCellID) as! StarsTableViewCell
            homeCell.selectionStyle = .none
            print("数据1",indexPath.row)
            homeCell.homeItem = items[indexPath.row]
            
            return homeCell
           
        }
        else if (type == 1){
            
            let homeCell = tableView.dequeueReusableCell(withIdentifier: StarsCellID) as! StarsTableViewCell
            homeCell.selectionStyle = .none
            print("数据2",indexPath.row)
            homeCell.homeItem = items[indexPath.row]
            
            return homeCell
        }
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        return cell
    }

}

你可能感兴趣的:(iOS-swift学习笔记-2)