在 UISearchController 中实现 updateSearchResultsForSearchController 时遇到问题?
Having trouble implementing updateSearchResultsForSearchController in UISearchController?
我很难在 UISearchController
中实现 updateSearchResultsForSearchController
。这与我如何实现我的原始数组有关。我不确定如何使用该数组来查找搜索到的文本。
这是我的代码片段:
Test.swift:
struct Test
{
let name: String
let hobby: String
}
Main.swift:
var resultsSearchController = UISearchController()
var filteredData: [Test] = [Test]()
var data: [Test] = [Test]()
override func viewDidLoad()
{
resultsSearchController = UISearchController(searchResultsController: nil)
definesPresentationContext = true
resultsSearchController.dimsBackgroundDuringPresentation = true
resultsSearchController.searchResultsUpdater = self
tableView.tableHeaderView = resultsSearchController.searchBar
data = [
Test(name: "Abby", hobby: "Games"),
Test(name: "Brian", hobby: "TV"),
Test(name: "Ced", hobby: "Gym"),
Test(name: "David", hobby: "Fun")]
tableView.dataSource = self
tableView.delegate = self
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if (resultsSearchController.active && resultsSearchController.searchBar.text != "")
{
return filteredData.count
}
return data.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell: UITableViewCell!
// Dequeue the cell to load data
cell = tableView.dequeueReusableCellWithIdentifier("Funny", forIndexPath: indexPath)
let example: Test
if resultsSearchController.active && resultsSearchController.searchBar.text != ""
{
example = filteredData[indexPath.row]
}
else
{
example = data[indexPath.row]
}
return cell
}
func updateSearchResultsForSearchController(searchController: UISearchController)
{
filteredData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "hobby CONTAINS[c] %@", resultsSearchController.searchBar.text!)
// WHAT ELSE TO DO?
tableView.reloadData()
}
我只是对如何使用 data
来 return 返回 updateSearchResultsForSearchController
中的正确搜索结果有点困惑。
有人能给我指出正确的方向吗?
谢谢
您需要做的是在您的数据源和 return 数据中进行搜索,仅此而已,类似于此代码:
func updateSearchResultsForSearchController(searchController: UISearchController) {
filteredData.removeAll(keepCapacity: false)
let searchedData = resultsSearchController.searchBar.text
// find elements that contains the searchedData string for example
filteredData = data.filter { [=10=].name.containsString(searchedData) }
tableView.reloadData()
}
如果您想对 struct
的另一个字段执行另一种类型的搜索,您可以随意修改 filter
。调用 tableView.reloadData()
后,所有内容都将重新加载。
希望对你有所帮助。
我很难在 UISearchController
中实现 updateSearchResultsForSearchController
。这与我如何实现我的原始数组有关。我不确定如何使用该数组来查找搜索到的文本。
这是我的代码片段:
Test.swift:
struct Test
{
let name: String
let hobby: String
}
Main.swift:
var resultsSearchController = UISearchController()
var filteredData: [Test] = [Test]()
var data: [Test] = [Test]()
override func viewDidLoad()
{
resultsSearchController = UISearchController(searchResultsController: nil)
definesPresentationContext = true
resultsSearchController.dimsBackgroundDuringPresentation = true
resultsSearchController.searchResultsUpdater = self
tableView.tableHeaderView = resultsSearchController.searchBar
data = [
Test(name: "Abby", hobby: "Games"),
Test(name: "Brian", hobby: "TV"),
Test(name: "Ced", hobby: "Gym"),
Test(name: "David", hobby: "Fun")]
tableView.dataSource = self
tableView.delegate = self
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if (resultsSearchController.active && resultsSearchController.searchBar.text != "")
{
return filteredData.count
}
return data.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell: UITableViewCell!
// Dequeue the cell to load data
cell = tableView.dequeueReusableCellWithIdentifier("Funny", forIndexPath: indexPath)
let example: Test
if resultsSearchController.active && resultsSearchController.searchBar.text != ""
{
example = filteredData[indexPath.row]
}
else
{
example = data[indexPath.row]
}
return cell
}
func updateSearchResultsForSearchController(searchController: UISearchController)
{
filteredData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "hobby CONTAINS[c] %@", resultsSearchController.searchBar.text!)
// WHAT ELSE TO DO?
tableView.reloadData()
}
我只是对如何使用 data
来 return 返回 updateSearchResultsForSearchController
中的正确搜索结果有点困惑。
有人能给我指出正确的方向吗?
谢谢
您需要做的是在您的数据源和 return 数据中进行搜索,仅此而已,类似于此代码:
func updateSearchResultsForSearchController(searchController: UISearchController) {
filteredData.removeAll(keepCapacity: false)
let searchedData = resultsSearchController.searchBar.text
// find elements that contains the searchedData string for example
filteredData = data.filter { [=10=].name.containsString(searchedData) }
tableView.reloadData()
}
如果您想对 struct
的另一个字段执行另一种类型的搜索,您可以随意修改 filter
。调用 tableView.reloadData()
后,所有内容都将重新加载。
希望对你有所帮助。