使用UISearchBar过滤数组 [英] Filter Array with UISearchBar

查看:79
本文介绍了使用UISearchBar过滤数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用以下代码过滤数组并将结果显示在tableView中.问题在于,仅当搜索与确切的单词匹配时,它才返回结果.如何在输入字符时更改数组过滤器以搜索每个字符?

I am currently using the following code to filter the array and present the results in my tableView. The problem is that this only returns results if the search matches the exact word. How do I change my array filter to search each character as I type it?

let data = ["Mango", "Grape", "Berry", "Orange", "Apple"]

var filteredData: [String] = []

filteredData = data.filter({$0 == searchBar.text})

使"Mango"显示在我的tableView中的唯一方法是,我必须在搜索栏中键入Mango,键入"Man"不会显示任何结果.

The only way to get "Mango" to show up in my tableView, I have to type Mango in the search bar, typing "Man" doesn't show any results.

推荐答案

尝试一下:

filteredData  = data.filter { $0.contains(_ other: searchBar.text) }

这篇关于使用UISearchBar过滤数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆