-2

I need your help. I'm using a textField to open a pickerView (and I did it successfully). But what I really need to do is put a textfield on the pickerview (or any place) and make it a SearchTextField. Is it possible?

This is my code so far:

class pruebaPickerViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{

    @IBOutlet weak var textField: TextField!
    var list = ["iPhone 6s", "iPhone 6s Plus", "iPhone SE"]


    override func viewDidLoad() {
        super.viewDidLoad()
        var pickerView = UIPickerView()
        pickerView.delegate = self
        pickerView.dataSource = self
        textField.inputView = pickerView
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{
        return list.count
    }

    func pickerView(_ pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
        return list[row]
    }

    func pickerView(_ pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
    {
        textField.text = list[row]
    }
}
Stephy Samaniego
  • 242
  • 1
  • 3
  • 15
  • 1
    You need to do a custom UI – Shehata Gamal Sep 25 '19 at 20:57
  • Why would you put a text field in the picker view? Do you mean you want each row of the picker view to be a text field? Or do you want to add one text field somewhere on/near the picker view? Please [edit] your question and clarify what you are trying to do and why. – rmaddy Sep 25 '19 at 20:57
  • Hi, thanks for the reply, i need to make a Search of the picker view elements. Before i was using the SearchTextField (https://github.com/apasccon/SearchTextField) but it does not simulate the material behavior with the placeholder :/ – Stephy Samaniego Sep 25 '19 at 21:07
  • add UIpickerViw in keyboard and in keyboard accessory view add a textField simple :) – Abu Ul Hassan Sep 26 '19 at 04:47

1 Answers1

0

Please try it for solution

class ViewController: UIViewController ,UIPickerViewDelegate, UIPickerViewDataSource, UISearchBarDelegate{

    var data = ["San Francisco","New York","San Jose","Chicago","Los Angeles","Austin","Seattle"]
    var filtered:[String] = []
    var pickerView = UIPickerView()
    var searchActive : Bool = false


    @IBOutlet weak var searchBar: UISearchBar!

    override func viewDidLoad() {
        super.viewDidLoad()

        pickerView.delegate = self
        pickerView.dataSource = self
        searchBar.inputAccessoryView = pickerView
    }


    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
        if(searchActive) {
            return filtered.count
        }
        return data.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        if searchActive == true{
            return filtered[row]
        }else{
            return data[row]
        }
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        if searchActive == true{
            searchBar.text = filtered[row]
        }else{
            searchBar.text = data[row]
        }
    }

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

        filtered = data.filter({ (text) -> Bool in
            let tmp = text
//            let range = tmp.rangeOfString(searchText, options: NSString.CompareOptions.CaseInsensitiveSearch)
            let range = tmp.range(of: searchText, options: NSString.CompareOptions.caseInsensitive)
            return ((range?.lowerBound) != nil)
        })
        if(filtered.count == 0){
            searchActive = false;
        } else {
            searchActive = true;
        }
        self.pickerView.reloadAllComponents()
    }
}

koen
  • 5,383
  • 7
  • 50
  • 89