【问题标题】:Filtering Apostrophes (special characters) in UISearchBar - Swift, Xcode在 UISearchBar 中过滤撇号(特殊字符) - Swift,Xcode
【发布时间】:2021-02-15 10:12:37
【问题描述】:

我目前有一个运行良好的 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 中输入撇号时控制台返回的输出: 过滤数据 = []

【问题讨论】:

  • 这是因为您的数组中没有任何标题为“Bob's”的人,搜索本身按预期工作。在操场上执行print("Bob's".range(of: "Bob'", options: .caseInsensitive) != nil) 并亲眼看看。
  • 您好,很抱歉信息不足。我已经编辑了我的帖子以包含更多信息。问题不在于“鲍勃”不在数组中,您将在上面看到。
  • 我不确定新代码对问题的真正添加。你是说 UISearchBar 坏了还是?完成过滤后,可以尝试直接调试或添加print(filteredData)
  • 您好,感谢您的回复。我实质上是说 UISearchBar 没有过滤撇号,我不知道为什么。除此之外,UISearchBar 运行良好。让我知道我还能提供什么来进一步帮助解决这个问题
  • 我按照建议添加了一个打印语句,当我输入撇号时,返回的输出是:filteredData []

标签: ios swift xcode uisearchbar uisearchcontroller


【解决方案1】:

这个问题已经解决了。事实证明,撇号有多种变体。有 U+0027 (') 和 U+2019 (')。 U+2019 (') 是 iPhone 上的默认撇号,可与上面的 UISearchBar 代码一起使用。希望这对和我有同样问题的人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    相关资源
    最近更新 更多