UIPickerView封裝,具有多種類型"單列數(shù)據(jù),多列不相關(guān)數(shù)據(jù),多列相關(guān)數(shù)據(jù),地址選擇,時(shí)間選擇"

ConvenientPickerView

一款方便的PickerView,通過(guò)簡(jiǎn)單的代碼設(shè)置可以實(shí)現(xiàn)UIPickerView,無(wú)需代理設(shè)置,回收之后還會(huì)移除,可以設(shè)置有兩種彈出,一種是UITextField彈出的,另一種是直接彈出.

anran.gif

如何使用ConvenientPickerView

  1. 這里使用這個(gè)PickerView最重要的是數(shù)據(jù)源,一定要是這種數(shù)據(jù)源
  let singleData = ["swift", "ObjecTive-C", "C", "C++", "java", "php", "python", "ruby", "js"]
  // 每一列為數(shù)組
  let multipleData = [["1天", "2天", "3天", "4天", "5天", "6天", "7天"],["1小時(shí)", "2小時(shí)", "3小時(shí)", "4小時(shí)", "5小時(shí)"],  ["1分鐘","2分鐘","3分鐘","4分鐘","5分鐘","6分鐘","7分鐘","8分鐘","9分鐘","10分鐘"]]

  // 注意這個(gè)數(shù)據(jù)的格式!
  let multipleAssociatedData: [[[String: [String]?]]] = [// 數(shù)組

  [   ["交通工具": ["陸地", "空中", "水上"]],//字典
      ["食品": ["健康食品", "垃圾食品"]],
      ["游戲": ["益智游戲", "角色游戲"]]
      
  ],// 數(shù)組
  
  [   ["陸地": ["公交車", "小轎車", "自行車"]],
      ["空中": ["飛機(jī)"]],
      ["水上": ["輪船"]],
      ["健康食品": ["蔬菜", "水果"]],
      ["垃圾食品": ["辣條", "不健康小吃"]],
      ["益智游戲": ["消消樂(lè)", "消滅星星"]],
      ["角色游戲": ["lol", "cf"]]
      
  ]
]

2.. ConvenientPickerView提供了多種類方法,我們可以通過(guò)類方法直接調(diào)用

        ConvenientPickerView.showSingleColPicker("單列數(shù)據(jù)",
                                                 data: singleData,
                                                 defaultSelectedIndex: 2) { [unowned self]
                                                    (selectIndex, selectValue) in
                                                    self.selectedLabel.text = "選中了第\(selectIndex)行----選中的數(shù)據(jù)為\(selectValue)"
        }
        ConvenientPickerView.showMultipleColsPicker("多列不關(guān)聯(lián)數(shù)據(jù)",
                                                    data: multipleData,
                                                    defaultSelectedIndexs: [0,1,2]) {[unowned self] (selectedIndexs, selectedValues) in
                                                        self.selectedLabel.text = "選中了第\(selectedIndexs)行----選中的數(shù)據(jù)為\(selectedValues)"
        }
        // 注意這里設(shè)置的是默認(rèn)的選中值, 而不是選中的下標(biāo),省得去數(shù)關(guān)聯(lián)數(shù)組里的下標(biāo)
        ConvenientPickerView.showMultipleAssociatedColsPicker("多列關(guān)聯(lián)數(shù)據(jù)", data: multipleAssociatedData, defaultSelectedValues: ["交通工具","陸地","自行車"]) {[unowned self] (selectedIndexs, selectedValues) in
            self.selectedLabel.text = "選中了第\(selectedIndexs)行----選中的數(shù)據(jù)為\(selectedValues)"
        }
        // 注意設(shè)置默認(rèn)值得時(shí)候, 必須設(shè)置完整, 不能進(jìn)行省略 ["四川", "成都", "成華區(qū)"] 比如不能設(shè)置為["四川", "成都"]
        // ["北京", "通州"] 不能設(shè)置為["北京"]
         ConvenientPickerView.showCitiesPicker("省市區(qū)選擇",
                                              defaultSelectedValues:  ["北京", "/", "/"],
                                              selectTopLevel: true) { [unowned self] (selectedIndexs, selectedValues) in
                                                // 處理數(shù)據(jù)
                                                let combinedString = selectedValues.reduce("", { (result, value) -> String in
                                                    result + " " + value
                                                })
                                                
                                                self.selectedLabel.text = "選中了第\(selectedIndexs)行----選中的數(shù)據(jù)為\(combinedString)"
        }
       ConvenientPickerView.showDatePicker("日期選擇") {[unowned self] ( selectedDate) in
           let formatter = DateFormatter()
           formatter.dateFormat = "yyyy-MM-dd"
           let string = formatter.string(from: selectedDate)
           self.selectedLabel.text = "選中了的日期是\(string)"
       }

3.. ConvenientPickerView關(guān)于UITextField的彈出

        // 代碼生成
        let test = PickerTextField(frame: CGRect(x: 20, y: timeTextField.frame.maxY, width: 340, height: 28))
        test.borderStyle = .roundedRect
        test.placeholder = "代碼初始化"
        test.showSingleColPicker("測(cè)試代碼", data: singleData, defaultSelectedIndex: 0, autoSetSelectedText: true) { [unowned self] (textField, selectedIndex, selectedValue) in
            print(selectedValue)
            self.selectedDataLabel.text = selectedValue
        }
        view.addSubview(test)
       // 使用Xib的情況:    
       @IBOutlet weak var singleTextField: PickerTextField!
       // 如果設(shè)置了autoSetSelectedText為true 將自動(dòng)設(shè)置text的值, 默認(rèn)以空格分開(kāi)多列選擇, 但你仍然可以在響應(yīng)完成的closure中修改text的值
       singleTextField.showSingleColPicker("編程語(yǔ)言選擇", data: singleData, defaultSelectedIndex: 2, autoSetSelectedText: true) {[unowned self] (textField, selectedIndex, selectedValue) in
            //  可以使用textField 也可以使用 self.singleTextField
            textField.text = "選中了第\(selectedIndex)行----選中的數(shù)據(jù)為\(selectedValue)"
            self.selectedDataLabel.text = "選中了第\(selectedIndex)行----選中的數(shù)據(jù)為\(selectedValue)"
            
        }

書(shū)寫(xiě)思路

  1. 我將工具欄單獨(dú)封裝為T(mén)oolBarView

  2. PickeView為單獨(dú)的View

  3. 最后在ConvenientPickerView, PickerTextField中調(diào)用快速構(gòu)建代碼

總結(jié)

本文是在仿寫(xiě)大神的Demo,通過(guò)練習(xí)封裝來(lái)提高自己的技術(shù),這里有完成的代碼,如果你覺(jué)得不錯(cuò)的話,給一個(gè)星星源碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,013評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,257評(píng)論 4 61
  • 一個(gè)人死了所有的事情都蓋棺定論了。 死的時(shí)候不能帶走錢(qián),不能帶在房子,不能帶走豪華的汽車,也不能帶走你漂亮的炮友。...
    貓黍閱讀 1,417評(píng)論 0 0
  • 月光下 心影重重疊
    谷間閱讀 273評(píng)論 0 0

友情鏈接更多精彩內(nèi)容