danutha
danutha

Reputation: 214

Not getting filter data in Custom UISearchBar

I'm working on a project in swift 3 and I have a specific UIViewController where I have a UITableView and as to populate data on its cell, the data is get from the server and I assign it to an array of the type JSON. I wants to implement a custom UISearchBar as to filter these data. I have worked partially on code and its as bellow.

import UIKit
import SwiftyJSON
import SDWebImage


class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating,UISearchBarDelegate {    

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var searchBar: UISearchBar!    
    var searchActive : Bool?
    var selectedCategoryList = [JSON?]()
    var filterSelectedCategoryList = [JSON?]()
    var lab :String?
    let searchController = UISearchController(searchResultsController: nil)
    override func viewDidLoad() {
    super.viewDidLoad()
            passJson()

     self.searchBar.delegate = self
            self.tableView.reloadData()
    }

            func updateSearchResults(for searchController: UISearchController) {
    }
        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            filterSelectedCategoryList = selectedCategoryList.filter { titles in
                return (titles?["healerName"].stringValue.lowercased().contains(searchText.lowercased()))!
            }
            tableView.reloadData()
            print("Array : ", filterSelectedCategoryList)
        }

     func passJson() {
            let path : String = Bundle.main.path(forResource: "JsonFile", ofType: "json") as String!

            let jsonData = NSData(contentsOfFile: path) as NSData!

            let readableJson = JSON(data: jsonData as! Data, options: JSONSerialization.ReadingOptions.mutableContainers, error: nil)

            let json = readableJson

             print(json)

            let selectedItemArray = json["itemList"].arrayValue
            self.selectedCategoryList = selectedItemArray
            print("new prints",self.selectedCategoryList)
            tableView.reloadData()
            // print( "Count",selectedItemArray?.count as Any)


            self.count = self.selectedCategoryList.count
            print(self.count)


        }


        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }


        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            if self.searchController.isActive && self.searchBar.text != ""{
                return filterSelectedCategoryList.count
            }else{
                return selectedCategoryList.count
            }

        }

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

            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell
            if count > 0 {
                if self.searchController.isActive && self.searchBar.text != ""{

                    cell.label.text = filterSelectedCategoryList[indexPath.row]?["healerName"].stringValue


                }else{
                    cell.label.text = selectedCategoryList[indexPath.row]?["healerName"].stringValue
                    cell.textLabel?.text = selectedCategoryList[indexPath.row]?["id"].stringValue
                    //cell.textLabel?.text=
                    self.lab = cell.textLabel?.text

                    cell.textLabel?.isHidden = true
                }


            }

            return cell
        }

Upvotes: 0

Views: 117

Answers (1)

Nirav D
Nirav D

Reputation: 72410

From your code you are working with UISearchBar not with UISearchController, so you need to only compare searchBar text in methods numberOfRowsInSection and in cellForRowAt, don't compare searchController.isActive with it.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if self.searchBar.text != ""{
        return filterSelectedCategoryList.count
    }else{
        return selectedCategoryList.count
    }        
}

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell
    if self.searchBar.text != ""{
        cell.label.text = filterSelectedCategoryList[indexPath.row]?["healerName"].stringValue

    }else{
        cell.label.text = selectedCategoryList[indexPath.row]?["healerName"].stringValue
        cell.textLabel?.text = selectedCategoryList[indexPath.row]?["id"].stringValue
        //cell.textLabel?.text=
        self.lab = cell.textLabel?.text

        cell.textLabel?.isHidden = true
    }
    return cell
}

Upvotes: 1

Related Questions