ios - mvvm 中的 UITextfield 委托(delegate)方法

标签 ios swift mvvm uitextfielddelegate

我需要在 mvvm 中搜索 tableview。

我的 View 模型:- 搜索功能:-

 func search(searchText :String?) {

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

  }

在 View Controller 中,我刚刚给出了文本字段委托(delegate)方法。

我的 View Controller :-

 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
    }




}

所以在文本字段中委托(delegate)我如何搜索。如何从 viewmodel 中获取搜索功能。搜索 tableview 任务是如何发生的

最佳答案

就在你的shouldChangeCharactersIn你可以调用searchViewModel搜索方法,如果它没有触发自动更新你的数据源,你需要在调用搜索方法后触发你的数据源

   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;
    }

关于ios - mvvm 中的 UITextfield 委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50207288/

相关文章:

swift - 找不到“双转换/双转换.h”文件

c# - 将 tabitem 转换为窗口

c# - 当使用 MVVM 更改一行 DataGrid 时将更改应用于模型

iphone - 为什么 Xcode 5 不允许我为 iPhone 应用程序指定 iPad 图像? iTunes Artwork 图标呢?

ios - 禁止空输入

ios - 使用 NSBatchInsertRequest 的 CoreData

ios - 如何使用 Metal 在 iOS 上使用 GPU 和 CPU 之间的共享内存? (理想情况下 objective-c )

swift - 由于未捕获的异常 'NSInvalidArgumentException' 无法识别的选择器发送到实例 0x7fdab8596b40' 而终止应用程序

ios - 如何让我的 UISearchController 显示下面的单元格?

c# - View Model 的序列化和反序列化