过滤器不适用于 tableView 中的搜索栏 UITextField Swift

Filter not working for searchbar UITextField in tableView Swift

我已经为 tableView 实现了 searchbar(UITextField) 并定义了相同的 SectionList 数组,但完全不知道过滤不起作用。当我尝试打印 IndexData 它有数据但 tableFilterData 是空的。

This is the main line which shows empty. tableFilterdata = IndexData.filter({[=16=].names.contains(searchText) })

var IndexData = [SectionList]()`

var tableFilterdata = [SectionList]()`

var isSearch : Bool! = false`


struct SectionList {
        let letter : String
        let names : [String]
    }

主要功能

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
       searchTxt.resignFirstResponder()
       return true
    }

public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{

        let searchText  = searchTxt.text! + string

        print(IndexData)

        tableFilterdata =  IndexData.filter({[=12=].names.contains(searchText) })

        print(tableFilterdata)
        if(tableFilterdata.count == 0){
           isSearch = false
         }else{
           isSearch = true
        }


        tableView.reloadData()
        return true
    }

替换,

let searchText  = searchTxt.text! + string

        print(IndexData)

        tableFilterdata =  IndexData.filter({[=10=].names.contains(searchText.lowercased()) })

if string.isEmpty {
    searchText = String(searchText.dropLast())
}
else {
    searchText = textField.text!+string
}

print(IndexData)

self.IndexData.removeAll()
self.tableFilterdata.removeAll()
if searchText.count == 0 {
    self.tableFilterdata = self.IndexData
} else {
    tableFilterdata =  IndexData.filter{ ([=11=].names.contains(searchText)) }
}