使用Swift中的SearchBar进行搜索时,区分首字母而不是整个字符。

huangapple go评论78阅读模式
英文:

Search that distinguishes the first letters and not the entire character with SearchBar in Swift

问题

我有一个字符数组,当我搜索并输入“Co”时,例如,我会得到两个匹配项(我不想要这样)。我只想搜索以特定字符开头的内容。

我分享字符串数组和Swift中的textDidChange方法

例如:

listEmployed = ["Conejo","Tetera","Aviteon","Sacoja"]
    
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    isSearching = true
    arrayFilterEmployed = listEmployed
    if searchText.isEmpty == false {
        arrayFilterEmployed = listEmployed?.filter({$0.text.lowercased().range(of: searchText.lowercased(), options: .caseInsensitive) != nil})
    }
    
    if arrayFilterEmployed?.count == 0 {
        searchBar.showsCancelButton = false
        foundLabel?.isHidden = false
    }
    else {
        categoryTableView?.reloadData()
        foundLabel?.isHidden = true
    }
    categoryTableView?.reloadData()
}
英文:

I have an array of characters and when I search and enter "Co" for example, I get two matches (I don't want that). I want only what begins with a specific character to be searched.

I share the array of strings and the method textDidChange in Swift

For example:

listEmployed = ["Conejo","Tetera","Aviteon","Sacoja"]
    
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    isSearching = true
    arrayFilterEmployed = listEmployed
    if searchText.isEmpty == false {
        arrayFilterEmployed = listEmployed?.filter({$0.text.lowercased().range(of: searchText.lowercased(), options: .caseInsensitive) != nil})
    }
    
    if arrayFilterEmployed?.count == 0 {
        searchBar.showsCancelButton = false
        fountLabel?.isHidden = false
    }
    else {
        categoryTableView?.reloadData()
        fountLabel?.isHidden = true
    }
    categoryTableView?.reloadData()
}

答案1

得分: 2

只需添加.anchored选项。由于您正在使用.caseInsensitive,因此不需要调用lowercased()

if !searchText.isEmpty {
    arrayFilterEmployed = listEmployed?.filter({$0.text.range(of: searchText, options: [.caseInsensitive, .anchored]) != nil})
}

.anchored表示检查只会在字符串的开头通过。如果选项还包括.backwards,那么.anchored将意味着匹配必须位于字符串的末尾。

英文:

You just need to add the .anchored option. And since you are using .caseInsensitive, you don't need the calls to lowercased():

if !searchText.isEmpty {
    arrayFilterEmployed = listEmployed?.filter({$0.text.range(of: searchText, options: [.caseInsensitive, .anchored]) != nil})
}

.anchored means the check will only pass if at the start of the string. If the options also included .backwards, then .anchored would mean the match must be at the end of the string.

答案2

得分: 1

你可以使用hasPrefix来过滤数据,在搜索栏中输入的文本将会根据此进行数据筛选。

用以下行替换:

arrayFilterEmployed = listEmployed?.filter({$0.text.lowercased().range(of: searchText.lowercased(), options: .caseInsensitive) != nil})

使用以下内容(区分大小写的搜索)进行替换:

arrayFilterEmployed = listEmployed?.filter({$0.text.hasPrefix(searchText) != nil})

并使用以下内容(不区分大小写的搜索)进行替换:

arrayFilterEmployed = listEmployed?.filter({$0.text.lowercased().hasPrefix(searchText.lowercased()) != nil})
英文:

In your case, you can filter data by using the hasPrefix. It will filter out the data based on the text you entered in the search bar.

Replace the following line:

arrayFilterEmployed = listEmployed?.filter({$0.text.lowercased().range(of: searchText.lowercased(), options: .caseInsensitive) != nil})

with (case-sensitive search)

arrayFilterEmployed = listEmployed?.filter({$0.text.hasPrefix(searchText) != nil})

and replace with the following (case-insensitive search)

arrayFilterEmployed = listEmployed?.filter({$0.text.lowercased().hasPrefix(searchText.lowercased()) != nil})

huangapple
  • 本文由 发表于 2023年7月20日 09:13:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定