Home:ALL Converter>Error with UIsearchbar results while selecting from tableview in swift iOS

Error with UIsearchbar results while selecting from tableview in swift iOS

Ask Time:2018-06-08T22:01:14         Author:vasu k

Json Formatter

In my current application, I show a list of students. I give a check mark button for selection. I am able to select students from the table, but whenever I try to select from search bar results, it's not working correctly.

For example, in the list I selected the object at index 0 and 1; whenever I try to search something, the search bar results object at index 0 and 1 gets selected automatically. Why is that, and how can I fix it?

// MARK:- seachbar methods
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String){
        search_bar.showsCancelButton = true

        print("SERACH TEXT ",searchText)
        if searchText.isEmpty{
            is_searching = false
        } else {
            print(" search text %@ ",searchText as NSString)
            is_searching = true
            searchingDataArray.removeAllObjects()
            for index in 0 ..< studentsArray.count
            {
                var localDic :NSDictionary!
                localDic = studentsArray.object(at: index) as! NSDictionary
                let nameString = localDic["name"] as? NSString
                let regString = localDic["reg_no"] as? NSString

                if (nameString?.localizedCaseInsensitiveContains(searchText))! || (regString?.localizedCaseInsensitiveContains(searchText))!
                {
                    searchingDataArray.add(localDic)
                } else {
                    print("Not Exists")
                }
                print(" search text %@ ",searchingDataArray)
                self.tutorListView.reloadData()
            }
        }
    }
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar)  {
        searchBar.resignFirstResponder()
    }
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar)
    {
        is_searching = false
        search_bar.text = ""
        search_bar.showsCancelButton = false
        self.tutorListView.reloadData()
    }
    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        is_searching = false 
        self.tutorListView.reloadData()
    }


   func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SKTutorCell", for: indexPath) as! SKTutorCell

        var localDic :NSDictionary!

        if is_searching
        {
            localDic = searchingDataArray.object(at: indexPath.row) as! NSDictionary
        }
        else
        {
            localDic = studentsArray.object(at: indexPath.row) as! NSDictionary
        }

        cell.nameRegdNoLbl.text = localDic?.value(forKey: "name") as? String

        cell.checkMarkBtn.tag = indexPath.row
        cell.checkMarkBtn.addTarget(self, action: #selector(checkBoxSelection(_:)), for: .touchUpInside)

        if ((studentcheckArray[indexPath.row] as AnyObject).isEqual("0")) {
            cell.checkMarkBtn.setImage(UIImage(named:"UnCheckIcon"), for: .normal)
        }else{
            cell.checkMarkBtn.setImage(UIImage(named:"CheckIcon"), for: .normal)
        }

        cell.seeProfileBtn.tag = indexPath.row
        cell.seeProfileBtn.addTarget(self, action: #selector(seeProfilAction(_:)), for: .touchUpInside)
        return cell
    }

    @objc func checkBoxSelection(_ sender:UIButton)
    {
        let btnTag = sender.tag
        print("buttonRow--",btnTag)
        if ((studentcheckArray[btnTag] as AnyObject).isEqual("0")) {
            studentcheckArray[btnTag] = "1"
        }
        else {
            studentcheckArray[btnTag] = "0"

        }
        var NewArray = NSMutableArray()
        for i in 0..<studentcheckArray.count {
            if ((studentcheckArray[i] as AnyObject).isEqual("1")) {
                NewArray.add(studentcheckArray[i])
                print("NewArray",NewArray)
            }
        }
        if NewArray.count < studentcheckArray.count {
            selectAllBtn.setImage(#imageLiteral(resourceName: "UnCheckIcon"), for: .normal)
        }
        self.tutorListView.reloadData()
    }

Author:vasu k,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50762364/error-with-uisearchbar-results-while-selecting-from-tableview-in-swift-ios
yy