swift实现自定义datePicker,只查询年份和月份
swift实现自定义datePicker,只查询年份和月份
媛美一生 发表于2年前
swift实现自定义datePicker,只查询年份和月份
  • 发表于 2年前
  • 阅读 25
  • 收藏 0
  • 点赞 0
  • 评论 0

移动开发云端新模式探索实践 >>>   

//

//  SDDatePickerView.swift

//  OA

//

//  Created by HelloMac on 16/6/2.

//  Copyright © 2016年 HelloMac. All rights reserved.

//

import UIKit

class SDDatePickerView: UIPickerView,UIPickerViewDelegate,UIPickerViewDataSource {

    var todayIndexPath:NSIndexPath?

    var months:NSArray?

    var years:NSArray?

    var bigRowCount = 1000

    var minYear = 2008

    var maxYear = 2030

    var rowHeight = 44

    var componentNumber:Int = 2

    var MONTH:Int = 0

    var YEAR:Int = 1

    var LABEL_TAG:Int = 43

    //var numberOfComponents: Int

    override func awakeFromNib() {

        super.awakeFromNib()

        self.months = self.nameOfMonths()

        self.years = self.nameOfYears()

        self.todayIndexPath = self.todayPath()

        //设置代理

        self.delegate = self

        self.dataSource = self

        self.selectToday()

    }

    func date() -> NSDate {

        let monthCount:Int = (self.months?.count)!

        let month = self.months?.objectAtIndex((self.selectedRowInComponent(MONTH)) % monthCount) as! String

        let yearCount:Int = (self.years?.count)!

        let year = self.years?.objectAtIndex((self.selectedRowInComponent(YEAR)) % yearCount) as! String

        let str = "\(year)-\(month)"

        let formatter = NSDateFormatter()

        formatter.dateFormat = "yyyy-MMMM"

        let date = formatter.dateFromString(str)

        return date!

    }

    /**

        创建年和月的数据源

     */

    func nameOfMonths() -> NSArray {

        let dateFormatter = NSDateFormatter()

        return dateFormatter.standaloneMonthSymbols

    }

    func nameOfYears() -> NSArray {

        let years = NSMutableArray()

        for year in self.minYear..<self.maxYear {

            let yeatStr = "\(year)"

            years.addObject(yeatStr)

        }

        return years

    }

    /**

        代理方法

    */

    func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {

        return self.componentWidth()

    }

    func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {

        var selected:Bool = false

        if component == MONTH{

            let monthCount:Int = (self.months?.count)!

            let monthName:String = self.months?.objectAtIndex(row % monthCount) as! String

            let currentMonthName:String = self.currentMonthName()

            if monthName == currentMonthName {

                selected = true

            }

        }else{

            let yearCount:Int = (self.years?.count)!

            let yearName:String = self.years?.objectAtIndex(row % yearCount) as! String

            let currentYearName:String = self.currentYearName()

            if yearName == currentYearName {

                selected = true

            }

        }

        var returnView:UILabel?

        if view?.tag == LABEL_TAG {

            returnView = view as? UILabel

        }else{

            returnView = self.labelForComponent(component, Selected: selected)

        }

        returnView?.textColor = selected ? UIColor.blueColor() : UIColor.blackColor()

        returnView?.text = self.titleForRow(row, forComponent: component)

        return returnView!

    }

    func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {

        return (CGFloat)(rowHeight)

    }

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {

        return componentNumber

    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        if component == MONTH {

            return self.bigRowMonthCount()

        }

        return self.bigRowYearCount()

    }

    func selectToday() {

        self.selectRow(self.todayPath().row, inComponent: MONTH, animated: false)

        self.selectRow(self.todayPath().section, inComponent: YEAR, animated: false)

    }

    func todayPath() -> NSIndexPath {

        //row ==month  section ==year

        var row:Int = 0

        var section:Int = 0

        let currentmonth = self.currentMonthName()

        let currentYear = self.currentYearName()

        let count:Int = (self.months?.count)!

        for month in 0..<count{

            let cellMonth = months![month] as! String

            if cellMonth == currentmonth {

                let currentRow:Int = (self.months?.indexOfObject(cellMonth))!

                row = currentRow

                //let rowCount:Int = self.bigRowMonthCount()/2

                row = row + self.bigRowMonthCount()/2

                break

            }

        }

        let Ycount:Int = (self.years?.count)!

        for year in 0..<Ycount{

            let cellYear = years![year] as! String

            if cellYear == currentYear {

                let currentRow:Int = (self.years?.indexOfObject(cellYear))!

                section = currentRow

               // let rowCount:Int = self.bigRowYearCount()/2

                section = section + self.bigRowYearCount()/2

                break

            }

        }

        return NSIndexPath.init(forRow: row, inSection: section)

    }

    func bigRowMonthCount() -> NSInteger {

        return (self.months?.count)! * bigRowCount

    }

    func bigRowYearCount() -> NSInteger {

        return (self.years?.count)! * bigRowCount

    }

    func componentWidth() -> CGFloat {

        let numberOfComponent:CGFloat = (CGFloat)(self.componentNumber)

        return self.bounds.size.width / numberOfComponent

    }

    func titleForRow(row:Int ,forComponent component:Int) -> String {

        if component == MONTH {

            let monthCount:Int = (self.months?.count)!

            return (self.months?.objectAtIndex(row % monthCount))! as! String

        }

        let  yearCount:Int = (self.years?.count)!

        return (self.years?.objectAtIndex(row % yearCount))! as! String

    }

    func labelForComponent(component:Int , Selected selected:Bool) -> UILabel {

        let frame = CGRectMake(0, 0, self.componentWidth(), (CGFloat)(rowHeight))

        let label = UILabel.init(frame: frame)

        label.textAlignment = NSTextAlignment.Center

        label.backgroundColor = UIColor.clearColor()

        label.textColor = selected ? UIColor.blueColor() : UIColor.blackColor()

        label.font = UIFont.boldSystemFontOfSize(18)

        label.userInteractionEnabled = false

        label.tag = LABEL_TAG

        return label

    }

    func currentMonthName() -> String {

        let date = NSDate()

        let formatter = NSDateFormatter()

        formatter.dateFormat = "MMMM"

        return formatter.stringFromDate(date)

    }

    func currentYearName() -> String {

        let date = NSDate()

        let formatter = NSDateFormatter()

        formatter.dateFormat = "yyyy"

        return formatter.stringFromDate(date)

    }

}

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 15
博文 51
码字总数 11281
×
媛美一生
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: