title: search the tableview using mvvm
body: <p>I need to search in the tableview.I have tableview and textfield.While clicking on the textfield it must search the tableview.</p>
  
  <p>my model:-</p>
  
  <pre><code>     class SearchModel: NSObject {
  
  
   var restaurantname :String!
  
  
      init?(dictionary :JSONDictionary) {
  
          guard let name = dictionary["name"] as? String  else {
                  return
          }
  
          self.restaurantname = name
  
      }
  
  
  
  }
  </code></pre>
  
  <p>my viewmodel:-</p>
  
  <pre><code>class SearchViewModel: NSObject {
  
  
      var datasourceModel:SearchDataSourceModel
  
      init(withdatasource  newDatasourceModel: SearchDataSourceModel) {
          datasourceModel = newDatasourceModel
      }
  
  
      func datafordisplay(atindex indexPath: IndexPath) -&gt; SearchModel{
          return  datasourceModel.dataListArray![indexPath.row]
  
      }
  
      func numberOfRowsInSection(section:Int) -&gt; Int {
  
          return (datasourceModel.dataListArray?.count)!
      }
  
  
  
  
      func loadData(completion :@escaping (_ isSucess:Bool) -&gt; ()){
  
  
          loadFromWebserviceData { (newDataSourceModel) in
  
              if(newDataSourceModel != nil)
              {
  
  
  
  
                  self.datasourceModel = newDataSourceModel!
                  completion(true)
  
              }
              else{
                  completion(false)
              }
          }
      }
  
  
      //}
      func loadFromWebserviceData(completion :@escaping (SearchDataSourceModel?) -&gt; ()){
  
          //with using Alamofire  ..............
  
          Alamofire.request("http://www.example.com").validate(statusCode: 200..&lt;300).validate(contentType: ["application/json"]).responseJSON{ response in
  
  
              switch response.result{
  
              case .success(let data):
                  print("success",data)
  
  
                  let result = response.result
                  if     let wholedata = result.value as? [String:Any]{
  
  
                      if  let data = wholedata["data"] as? Array&lt;[String:Any]&gt;{
                          //  print(data["name"] as! String)
                          print(data)
                          print(response)
                          let newDataSource:SearchDataSourceModel = SearchDataSourceModel(array: data)
  
                          completion(newDataSource)
                          //       }
                      }
                  }
                  //  case .failure(let data):
                  //  print("fail",data)
  
  
              case .failure(let encodingError ):
                  print(encodingError)
  
  
                  //  if response.response?.statusCode == 404{
  
  
                  print(encodingError.localizedDescription)
  
                  completion(nil)
  
                  //   }
              }
  
  
  
  
          }}
  }
  </code></pre>
  
  <p>my datasource model:-</p>
  
  <pre><code>class SearchDataSourceModel: NSObject {
  
  
  
      var dataListArray:Array&lt;SearchModel&gt;? = []
  
  
  
  
      init(array :Array&lt;[String:Any]&gt;?) {
          super.init()
          var newArray:Array&lt;[String:Any]&gt; = []
          if array == nil{
  
           //   newArray = self.getJsonDataStored44()
          }
          else{
              newArray = array!
  
          }
  
          var datalist:Array&lt;SearchModel&gt; = []
          for dict in newArray{
  
              let model = SearchModel(dictionary: dict)
  
              datalist.append(model!)
          }
          self.dataListArray = datalist
  
  
            }
  
  
  
  }
  </code></pre>
  
  <p>my viewcontroller:-</p>
  
  <pre><code>class SearchViewController: UIViewController,UITableViewDelegate,UITableViewDataSource, UISearchBarDelegate,UITextFieldDelegate {
  
  
      @IBOutlet private weak var tableView: UITableView!
    !
      @IBOutlet weak var txt: UITextField!
  
      var filteredSearchArray = NSMutableArray()
  
  
  
      private var searchViewModel :SearchViewModel!
  
      init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?, withViewModel viewModel:SearchViewModel) {
  
          super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  
          searchViewModel  = viewModel
      }
  
  
  
      required init?(coder aDecoder: NSCoder) {
          fatalError("init(coder:) has not been implemented")
      }
  
  
  
      var filteredData: [String]!
  
  
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
  
  
  
          txt.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
  
  
  
          searchViewModel.loadData { (isSuccess) in
  
  
              if(isSuccess == true)
              {
  
  
                  self.tableView .reloadData()
  
  
              }
              else{
  
  
                  }
          }
  
  
  
      }
  
  
      @objc private func textFieldDidChange(textField: UITextField) {
          if textField.text == "" {
             self .viewDidLoad()
          }else{
              filterContentForSearchText(searchText: textField.text!)
          }
      }
  
  
  
      func filterContentForSearchText(searchText: String) {
          filteredSearchArray = NSMutableArray(array:(searchViewModel.datasourceModel.dataListArray?.filter({(ele:AnyObject) -&gt; Bool in
              return (ele as! SearchModel).restaurantname.lowercased().contains(searchText.lowercased())
          }))!)
          tableView.reloadData()
      }
  
  
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return searchViewModel.numberOfRowsInSection(section: section)
      }
  
  
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
  
  
          let identifier = "searchcell"
          var cell: SearchCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? SearchCell
  
          if cell == nil {
              tableView.register(UINib(nibName: "SearchCell", bundle: nil), forCellReuseIdentifier: identifier)
              cell = tableView.dequeueReusableCell(withIdentifier: identifier) as?SearchCell
          }
  
  
     cell.setsearchData(search: searchViewModel.datafordisplay(atindex: indexPath))
  
  
  
          return cell
      }
  
  
  }
  </code></pre>
  
  <p>my tableviewcell:-</p>
  
  <pre><code>class SearchCell: UITableViewCell {
  
  
  
  
    @IBOutlet weak var name: UILabel!
  
  
      override func awakeFromNib() {
          super.awakeFromNib()
          // Initialization code
      }
  
      override func setSelected(_ selected: Bool, animated: Bool) {
          super.setSelected(selected, animated: animated)
  
          // Configure the view for the selected state
      }
  
  
      func setsearchData(search:SearchModel)
      {
          self.name.text = search.restaurantname
  
  
      }
  }
  </code></pre>
  
  <p>This is my code but in the filteredSearchArray always shows as 0 element.</p>
  
  <p>Here the list of names is displaying but clicking on the textfield it doesn't searching and not reloading. How to solve the problem.</p>
  
  <p>The data is not coming  in this code:-</p>
  
  <pre><code> func filterContentForSearchText(searchText: String) {
          filteredSearchArray = NSMutableArray(array:(searchViewModel.datasourceModel.dataListArray?.filter({(ele:AnyObject) -&gt; Bool in
              return (ele as! SearchModel).restaurantname.lowercased().contains(searchText.lowercased())
          }))!)
          tableView.reloadData()
      }
  </code></pre>