KUNWOO MYUNG
KUNWOO MYUNG

Reputation: 215

Swift3 UISearchBar isn't working right

I use UISearchBar for searching address book & showing the result.

In my address book, There is Kate Bell, Anna Haro, Daniel Higgins etc.

First try: "Kate Bell" Shows me right.

enter image description here

Second try: "Anna Haro" Shows me Kate & Anna.

enter image description here

Then Third try: "Daniel Higgins" ... from this moment UISearchBar Can't give me right results....

enter image description here

I have no idea What is the problem & How to solve this....

I'm attaching my swift code below..

var searchActive : Bool = false
var data:[String] = []
var filtered:[String] = []

override func viewDidLoad() {
    super.viewDidLoad()

    searchBar.delegate = self
    friendsTableView.delegate = self
    friendsTableView.dataSource = self
}

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

    if(searchActive) {
        return filtered.count
    }
    return contacts.count;
}

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

    let cell : ContactTableViewCell = tableView.dequeueReusableCell(withIdentifier: "ContactTableViewCell", for: indexPath) as! ContactTableViewCell
    let cell2 = tableView.dequeueReusableCell(withIdentifier: "ContactTableViewCell")! as UITableViewCell;

    let entry = contacts[(indexPath as NSIndexPath).row]

    cell.configureWithContactEntry(entry)
    cell.layoutIfNeeded()

    if(searchActive){
        cell2.textLabel?.text = filtered[indexPath.row]
    } else {
        cell2.textLabel?.text = data[indexPath.row]
    }

    return cell
}

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    searchActive = true;
}

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    searchActive = false;
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searchActive = false;
}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchActive = false;
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    filtered = data.filter({ (text) -> Bool in
        let tmp: NSString = text as NSString
        let range = tmp.range(of: searchText, options: NSString.CompareOptions.caseInsensitive)
        return range.location != NSNotFound
    })
    if(filtered.count == 0){
        searchActive = false;
    } else {
        searchActive = true;
    }
    self.friendsTableView.reloadData()
}

Upvotes: 0

Views: 259

Answers (1)

Dark Innocence
Dark Innocence

Reputation: 1389

Here's my approach:-

    var searchText = ""

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

        if searchText.isEmpty || searchText == "" {

            filtered = data
            self.friendsTableView.reloadData()
            searchText = searchText
        }

        else {

            searchtext = searchText
            filterTableView(text: searchText)
        }
    }


func filterTableView(text: String) {      

    filtered = data.filter({ (dis) -> Bool in
                return dis.data.lowercased().contains(text.lowercased())
            })

            self.friendsTableView.reloadData()       
        }

Upvotes: 3

Related Questions