内含: 1、对UIPickerDelegate和UIPickerDataSource的使用说明;2、使用Bundle获取本地plist文件,解析Plist文件;3、使用SnapKit对视图自动布局
如图所示
import UIKit class WYSynMainViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { // 自定义视图 lazy var syntime_tabarView: WYCustomSynTime_Tabar = { let _tabarView = WYCustomSynTime_Tabar(frame: .zero) return _tabarView }() // pickerView lazy var syntime_pickerViwe: UIPickerView = { let pickerView = UIPickerView(frame: .zero) pickerView.delegate = self pickerView.dataSource = self return pickerView }() private var minuteArray: [String]? // 分钟数组 private var hourArray: [String]? // 小时数组 private var mainTitle: String? // 标题 private var currentHour: String? // 被选中时,小时 private var currentMinute: String? // 被选中时,分钟 override func viewDidLoad() { super.viewDidLoad() setDataSource() // 设置数据 setUpUI() // 设置UI aciton() // 设置自定义视图事件回调,采用的Block } // 设置数据 private func setDataSource() { if let path = Bundle.main.path(forResource: "SynTimePlistFile", ofType: "plist") { if let _dic = NSDictionary(contentsOfFile: path) as? Dictionary<String, AnyObject> { mainTitle = _dic["title"] as? String hourArray = _dic["hour"] as? [String] minuteArray = _dic["minute"] as? [String] } } } // 设置UI private func setUpUI() { view.backgroundColor = UIColor.white navigationItem.title = "时间设置" weak var weakSelf = self guard let _weakSelf = weakSelf else{return} view.addSubview(syntime_tabarView) syntime_tabarView.snp.makeConstraints { (make) in make.left.right.bottom.equalTo(_weakSelf.view) make.height.equalTo(49) } view.addSubview(syntime_pickerViwe) syntime_pickerViwe.snp.makeConstraints { (make) in make.left.right.equalTo(_weakSelf.view) make.centerY.equalTo(_weakSelf.view) } } // 设置自定义视图回调函数 private func aciton() { syntime_tabarView.sytimeCustomTabarButtonActionHandle = {(button) in if button.tag == 1 { } if button.tag == 2 { } } } // MARK: UIPickerViewDelegate & UIPickerViewDataSource // 设置组的数量 func numberOfComponents(in pickerView: UIPickerView) -> Int { return 3 } // 根据组下标,设置组里item的数量 func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { switch component { case 0: return 1 case 1: return 12 case 2: return 60 default: break } return 0 } // 根据组下标,设置item文字 func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { switch component { case 0: return mainTitle case 1: return hourArray?[row] case 2: return minuteArray?[row] default: break } return nil } // 根据组下标返回一个视图 // func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { // // } // 当前选择的下标 func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { switch component { case 1: currentHour = hourArray?[row] case 2: currentMinute = minuteArray?[row] default: break } } // 根据组下标或则item下标,设置字体熟悉 func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { return nil } // 设置设置每组的宽度 func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat { switch component { case 0: return UIScreen.main.bounds.width / 4 * 2 case 1: return UIScreen.main.bounds.width / 4 case 2: return UIScreen.main.bounds.width / 4 default: break } return 0.0 } }
时间: 2024-11-07 20:48:36