Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios mvvm中的UITextfield委托方法_Ios_Swift_Mvvm_Uitextfielddelegate - Fatal编程技术网

Ios mvvm中的UITextfield委托方法

Ios mvvm中的UITextfield委托方法,ios,swift,mvvm,uitextfielddelegate,Ios,Swift,Mvvm,Uitextfielddelegate,我需要在mvvm中搜索tableview 我的viewmodel:- 搜索功能:- func search(searchText :String?) { filteredListArray = datasourceModel.dataListArray?.filter{($0.restaurantname?.range(of: searchText!, options: .caseInsensitive) != nil)} } 在viewcontroller中,我刚刚

我需要在mvvm中搜索tableview

我的viewmodel:- 搜索功能:-

 func search(searchText :String?) {

        filteredListArray = datasourceModel.dataListArray?.filter{($0.restaurantname?.range(of: searchText!, options: .caseInsensitive) != nil)}

  }
在viewcontroller中,我刚刚给出了textfield委托方法

我的viewcontroller:-

 class QM_SearchViewController: UIViewController,UITableViewDelegate,UITableViewDataSource, UISearchBarDelegate,UITextFieldDelegate {


    @IBOutlet private weak var tableView: UITableView!
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var txt: UITextField!

    var isSearching = false
    var search:String=""

    var filteredSearchArray = NSMutableArray()


    private var searchViewModel :QM_SearchViewModel!

    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?, withViewModel viewModel:QM_SearchViewModel) {

        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)

        searchViewModel  = viewModel
    }




    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "SEARCH"


        txt.delegate = self

     searchViewModel.loadData { (isSuccess) in


        if(isSuccess == true)
        {

            self.tableView.reloadData()

        }
        else{

            }
        }


    }

   func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {






        print("While entering the characters this method gets called")
       return true;
  }


    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {  //delegate method
        return false
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {   //delegate method
        textField.resignFirstResponder()

        return true
    }



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {



        return searchViewModel.numberOfRowsInSection(section: section)


}

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let identifier = "searchcell"
        var cell: QM_SearchCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? QM_SearchCell

        if cell == nil {
            tableView.register(UINib(nibName: "QM_SearchCell", bundle: nil), forCellReuseIdentifier: identifier)
            cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? QM_SearchCell
        }






       cell.setsearchData(search: searchViewModel.datafordisplay(atindex: indexPath))



        return cell
    }




}

因此,在textfield中,代表我如何进行搜索。如何从viewmodel中获取搜索功能。搜索tableview任务如何发生

您的
应更改字符在
中,您可以调用
搜索视图模型
搜索方法,如果它不触发自动更新数据源,您需要在调用搜索方法后触发数据源

   func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

        if let text = textField.text,
            let textRange = Range(range, in: text)  {
            let updatedText = text.replacingCharacters(in: textRange,
                                                       with: string)

            if updatedText.count > 2 {
                let result  = searchViewModel.search(searchText :updatedText)

                //  result Should update your data Source searchViewModel.numberOfRowsInSection , and reload tableView
            }
        }
            return true;
    }

将错误显示为:-无法使用类型为(NSRange,in:String)的参数列表调用类型范围的初始值设定项,在文本字段中对文本进行退格时,以及在键入文本后,输入键,然后,代码应如何使用count调用搜索方法如果count>2,例如,请参阅答案