-2

我需要你的帮助。我正在使用 textField 打开一个pickerView(并且我成功地做到了)。但我真正需要做的是在pickerview(或任何地方)上放置一个文本字段并将其设为SearchTextField。可能吗?

到目前为止,这是我的代码:

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]
    }
}
4

1 回答 1

0

请尝试解决

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()
    }
}

于 2019-09-26T13:10:55.167 回答