美文网首页IOS
日期选择器BRPickerView的使用

日期选择器BRPickerView的使用

作者: 瑟闻风倾 | 来源:发表于2019-01-21 20:20 被阅读0次

    BRPickerView

    1. 界面展示

    按日统计.png
    按月统计.png
    按年统计.png

    2. 界面设计

    界面设计.png

    3.具体实现

    //
    //  BillTableViewController.swift
    //  JackUChat
    //
    //  Created by 徐云 on 2019/1/15.
    //  Copyright © 2019 Liy. All rights reserved.
    //
    
    import UIKit
    import Alamofire
    import SwiftyJSON
    import Charts
    //import BRPickerView
    
    class BillTableViewController: UITableViewController {
        
        
        @IBOutlet weak var bgTimeBtn: UIButton!
        @IBOutlet weak var edTimeBtn: UIButton!
        @IBOutlet weak var searchBtn: UIButton!
        @IBOutlet weak var pieChartView: PieChartView!
        
        var bgTime = "2019-01-10"
        var edTime = "2019-01-16"
        
        var pageMenuTitle = ""
        
        var moneys:[String] = []
        var percents:[Double] = []
    
        var devices:[Device] = []
        
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Uncomment the following line to preserve selection between presentations
            // self.clearsSelectionOnViewWillAppear = false
    
            // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
            // self.navigationItem.rightBarButtonItem = self.editButtonItem
            
            
            let defaults = UserDefaults.standard
            pageMenuTitle = defaults.string(forKey: "page_menu_title")!
            print("BillTableViewController******PageView页面顶部滑动菜单:" + pageMenuTitle)
            getListByAlomafire(pageMenuTitle: pageMenuTitle)
            
            if (pageMenuTitle == "按日统计") {
                //bgTimeBtn.titleLabel!.text = "2019-01-10"
                //edTimeBtn.titleLabel!.text = "2019-01-16"
                bgTimeBtn.setTitle("2019-01-10", for: UIControl.State.init(rawValue: 0))
                bgTime = "2019-01-10"
                edTimeBtn.setTitle("2019-01-16", for: UIControl.State.init(rawValue: 0))
                edTime = "2019-01-16"
                getListByAlomafire(pageMenuTitle: pageMenuTitle)
            }else if (pageMenuTitle == "按月统计") {
                bgTimeBtn.setTitle("2019-01", for: UIControl.State.init(rawValue: 0))
                bgTime = "2019-01"
                edTimeBtn.isHidden = true
                edTime = ""
                getListByAlomafire(pageMenuTitle: pageMenuTitle)
            }else if (pageMenuTitle == "按年统计") {
                bgTimeBtn.setTitle("2019", for: UIControl.State.init(rawValue: 0))
                bgTime = "2019"
                edTimeBtn.isHidden = true
                edTime = ""
                getListByAlomafire(pageMenuTitle: pageMenuTitle)
            }
        }
        
        
        @IBAction func bgTimeBtnTap(_ sender: UIButton) {
            showDate(selectType: pageMenuTitle, btn: bgTimeBtn,btnType:"bg")
        }
        
        
        @IBAction func edTimeBtnTap(_ sender: UIButton) {
            showDate(selectType: pageMenuTitle, btn: edTimeBtn,btnType:"ed")
        }
        
        func showDate(selectType:String,btn:UIButton,btnType:String) {
            switch selectType {
            case "按日统计":
                BRDatePickerView.showDatePicker(withTitle: "日期选择", dateType: .YMD, defaultSelValue: "2018-06-15", minDate: nil, maxDate: nil, isAutoSelect: true, themeColor: UIColor.orange, resultBlock: { (result) in
                    print("确定")
                    btn.setTitle(result, for: UIControl.State.init(rawValue: 0))
                    if (btnType == "bg") {
                        self.bgTime = result!
                    }else if (btnType == "ed") {
                        self.edTime = result!
                    }
                }) {
                    print("取消")
                }
                break
            case "按月统计":
                BRDatePickerView.showDatePicker(withTitle: "日期选择", dateType: .YM, defaultSelValue: "2018-06", minDate: nil, maxDate: nil, isAutoSelect: true, themeColor: UIColor.orange, resultBlock: { (result) in
                    print("确定")
                    btn.setTitle(result, for: UIControl.State.init(rawValue: 0))
                    self.bgTime = result!
                }) {
                    print("取消")
                }
                break
            case "按年统计":
                BRDatePickerView.showDatePicker(withTitle: "日期选择", dateType: .Y, defaultSelValue: "2018", minDate: nil, maxDate: nil, isAutoSelect: true, themeColor: UIColor.orange, resultBlock: { (result) in
                    print("确定")
                    btn.setTitle(result, for: UIControl.State.init(rawValue: 0))
                    self.bgTime = result!
                }) {
                    print("取消")
                }
                break
            default:
                break
            }
            
        }
        
        
        @IBAction func searchBtnTap(_ sender: UIButton) {
            getListByAlomafire(pageMenuTitle: pageMenuTitle)
        }
        
        // MARK: - Table view data source
    
    //    override func numberOfSections(in tableView: UITableView) -> Int {
    //        // #warning Incomplete implementation, return the number of sections
    //        return 0
    //    }
    
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            // #warning Incomplete implementation, return the number of rows
            return devices.count
        }
    
        
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            //let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
    
            // Configure the cell...
            let cellId = String(describing: OrderCell.self)
            let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! OrderCell
            let device = devices[indexPath.row]
            cell.deviceNameLabel.text = device.deviceName
            cell.deviceNoLabel.text = device.deviceId
            cell.countLabel.text = "¥" + device.deviceCount
    
            return cell
        }
     
    
        /*
        // Override to support conditional editing of the table view.
        override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
            // Return false if you do not want the specified item to be editable.
            return true
        }
        */
    
        /*
        // Override to support editing the table view.
        override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
            if editingStyle == .delete {
                // Delete the row from the data source
                tableView.deleteRows(at: [indexPath], with: .fade)
            } else if editingStyle == .insert {
                // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
            }    
        }
        */
    
        /*
        // Override to support rearranging the table view.
        override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
    
        }
        */
    
        /*
        // Override to support conditional rearranging of the table view.
        override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
            // Return false if you do not want the item to be re-orderable.
            return true
        }
        */
        
        func getListByAlomafire(pageMenuTitle:String) {
            if (pageMenuTitle == "") {
                return
            }
            var tab = ""
            if (pageMenuTitle == "按日统计") {
                tab = "day"
            }else if (pageMenuTitle == "按月统计") {
                tab = "month"
                self.edTime = ""
            }else if (pageMenuTitle == "按年统计") {
                tab = "year"
                self.edTime = ""
            }
            
            let params:Parameters = ["tab":tab,"bg_time":bgTime,"end_time":edTime]
            AlamofireHelper.shareInstance.requestData(.post, url: "account/index", parameters: params) { (result) in
                let jsonDictory = JSON(result as Any)
                let code = jsonDictory["code"].string
                let msg = jsonDictory["msg"].string
                if(code == "0"){
                    print("成功:"+code!+","+msg!)
                    let nameList = jsonDictory["data"]["orderList"]["name"]
                    let snList = jsonDictory["data"]["orderList"]["sn"]
                    let dataList = jsonDictory["data"]["orderList"]["data"]
                    for index in 0...dataList.count - 1{
                        let money = "\(dataList[index])"
                        self.moneys.append(money)
                        self.percents.append(dataList[index].double!)
                    }
                    if (nameList.count == snList.count && nameList.count == dataList.count){
                        for index in 0...nameList.count - 1{
                            //重用了OrderCell,为了不新增金额字段,此接口将接收到的金额存储在deviceCount字段中
                            let device = Device(deviceId: snList[index].string ?? "", deviceName: nameList[index].string ?? "", deviceStatus: "", deviceCount: "\(dataList[index])" , deviceImage: "", date: "")
                            self.devices.append(device)
                        }
                    }
                    
                    dump(self.devices)//打印
                    //异步获取数据,需在主线程中更新
                    OperationQueue.main.addOperation {
                        self.setChart(dataPoints: self.moneys, values: self.percents)
                        self.tableView.reloadData()
                        self.tableView.refreshControl?.endRefreshing()//加载完数据后停止下拉刷新动画
                    }
                    
                }else{
                    print("失败")
                }
                
            }
            
        }
        
        func setChart(dataPoints:[String],values:[Double]) {
            var dataEntries:[PieChartDataEntry] = []
            for i in 0..<dataPoints.count {
                let dataEntry = PieChartDataEntry(value: values[i], label: moneys[i])
                dataEntries.append(dataEntry)
            }
            let chartDataSet = PieChartDataSet(values: dataEntries, label: "Units Sold")
            let chartData = PieChartData(dataSet: chartDataSet)
            pieChartView.data = chartData
            
            
        }
        
        
    
        /*
        // MARK: - Navigation
    
        // In a storyboard-based application, you will often want to do a little preparation before navigation
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // Get the new view controller using segue.destination.
            // Pass the selected object to the new view controller.
        }
        */
    
    }
    
    

    备注:获取的网络数据示例

    {
      "code": "0",
      "msg": "success",
      "data": {
        "orderList": {
          "name": [
            "门襟卷缝机(一楼测试2)",
            "问题测试  ",
            "经销商测试工厂中继",
            "折叠压烫机",
            "迈卡袖衩机",
            "老化测试04"
          ],
          "sn": [
            "MCD201793",
            "Qtest",
            "agentrelay",
            "10052017679",
            "0109",
            "LHCS04"
          ],
          "data": [
            0,
            1,
            0,
            0,
            5,
            5
          ]
        },
        "total": "11.00"
      }
    }
    

    相关文章

      网友评论

        本文标题:日期选择器BRPickerView的使用

        本文链接:https://www.haomeiwen.com/subject/ebshjqtx.html