从swift iOS中的tableview中选择时,UIsearchbar结果出错

时间:2018-06-08 14:01:14

标签: ios swift uisearchbar

在我目前的申请中,我会显示一份学生名单。我给了一个复选标记按钮进行选择。我可以从表格中选择学生,但每当我尝试从搜索栏结果中选择时,它都无法正常工作。

例如,在列表中我选择了索引0和1处的对象;每当我尝试搜索某些内容时,索引0处的搜索栏结果对象将自动被选中。为什么会这样,我该如何解决?

// 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()
    }

1 个答案:

答案 0 :(得分:1)

试试这个

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

if ((searchingDataArray[indexPath.row] as AnyObject).isEqual("0")) {

    cell.checkMarkBtn.setImage(UIImage(named:"UnCheckIcon"), for: .normal)
}else{
    cell.checkMarkBtn.setImage(UIImage(named:"CheckIcon"), for: .normal)
}

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


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.nameRegdNoLbl.text = localDic?.value(forKey: "name") as? String

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


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