1

I need to search the tableview in mvvm .

my viewmodel:- function for search:-

 func search(searchText :String?) {

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

  }

And in the viewcontroller i just given the textfield delegate method.

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




}

So in the textfield delegate HOW could i search.How could take the search function from viewmodel.How the searching tableview task take place

Shivam Tripathi
  • 1,405
  • 3
  • 19
  • 37

1 Answers1

1

Just in side your shouldChangeCharactersIn you can call searchViewModel Search Method , if it not trigger update your dataSource Automatically , you will need to trigger your data source after call search Method

   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;
    }
Abdelahad Darwish
  • 5,969
  • 1
  • 17
  • 35