首页 > 解决方案 > 在 UISearchBar 中过滤撇号(特殊字符) - Swift,Xcode

问题描述

我目前有一个工作正常的 UISearchBar,但是,我注意到在 searchBar 中输入撇号时,它不会返回任何结果。例如:如果我有一个字符串:Bob's如果我搜索Bob它会返回所述字符串,但是,只要我在 searchBar: Bob'中输入撇号,searchBar就不会返回任何结果。

我在网上搜索了解决方案,但是没有找到任何有效的方法。任何回应将不胜感激,谢谢。

UISearch条码:

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    if searchText.isEmpty {
        filteredData = data
    } else {
        filteredData = data.filter{$0.title.range(of: searchText, options: .caseInsensitive) != nil }
    }
    self.tableView.reloadData()
}

}

结构:

struct Category {
    let title: String

    let items: [String]
}

大批: Category(title: "Bob's ", items: ["Data: xxx", "Data: xxx", "Data: xxx",]),

cellForRowAt 函数:

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

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell

        let title = filteredData[indexPath.row].title

          cell.myLabel.text = title

          cell.myImg.image = UIImage(named: title + ".png")

          return cell

    }

}

在 searchBar 中键入撇号时在控制台中返回的输出: filteredData = []

标签: iosswiftxcodeuisearchbaruisearchcontroller

解决方案


这已解决。事实证明,撇号有多种变体。有 U+0027 (') 和 U+2019 (')。U+2019 (') 是 iPhone 上的默认撇号,适用于上面的 UISearchBar 代码。希望这可以帮助任何与我有相同问题的人。


推荐阅读